How to Redirect HTTP to HTTPS in Nginx

Nginx is a Linux-based web server and proxy application. Nginx is a powerful tool for redirecting and managing web traffic. It can be easily configured to redirect unencrypted HTTP web traffic to an encrypted HTTPS server.

  • A Linux server running Nginx
  • A user account with sudo privileges
  • Remote login to a web server (optional, required only if you are not working directly on your webserver)

HTTP to HTTPS Redirect

To enforce an HTTP to HTTPS redirect, you need to edit the Nginx configuration file.

In most cases, you can locate the file in the /etc/nginx/sites-available directory. If not found, search for it here: /etc/nginx/nginx.conf, /usr/local/nginx/conf, or /usr/local/etc/nginx.

Once you have located the Nginx configuration file, open it in a text editor with the command:

sudo nano /etc/nginx/sites-available/server.conf

Replace the location with the actual location and name of your configuration file.

Note: If you are connecting remotely, make sure you’re logged in through SSL first. Also, if you are using a graphical interface, you can browse to the file location instead of using terminal commands.

Once the configuration file is open for editing, insert one of the code blocks below. Once you are finished editing, save the file and exit. Then, restart the Nginx service with the following command:

sudo service nginx restart | systemctel restart nginx

Nginx Redirect all HTTP traffic to HTTPS

Open the Nginx configuration file for editing, then insert the following code:

server { listen 80 default_server; server_name _; return 301 https://$host$request_uri; }

Here is a breakdown of the commands:

  • Listen 80: This instructs the system to catch all HTTP traffic on Port 80
  • Server_name _;: This will match any hostname
  • Return 301: This tells the browser (and search engines) that this is a permanent redirect
  • https://$host$request_uri: This is a shortcode to specify the HTTPS version of whatever the user has typed

After editing, all traffic for the HTTP default server redirects to HTTPS.

Note: This should be the only server block listening on port 80. (A server block is a unit of configuration code in Nginx. It’s marked by a name and curly brackets.)

Redirect a Specific Site

You may have multiple servers, but only some of them may require HTTPS. Specify server name in the server block to redirect the selected traffic:

server { listen 80 default_server;server_name;return 301$request_uri; }

Replace the name with the name of the server you intend to redirect. You may also want to add additional sites by adding another server block. Simply copy the code, and switch out the name of the server.

Accept Only SSL Connections

Add this code to be sure that the server will only accept SSL connections on Port 443:

server { listen 443 ssl default_server; server_name; } server { listen 443 ssl; server_name; }

This code block will set two websites, and, to accept only SSL connections. You can add additional sites by using additional server blocks.

You can use the rewrite code to quickly manage a 301 (permanent) or 302 (temporary) redirect:

Location /index.html { rewrite ^/oldURL$ redirect; }

Most of the time, the location will be index.html, but you can specify any path/pattern.

Make note that the rewrite the command should only be used with 301 or 302 redirects.

This is useful if you have changed from a vanity extension (like .biz or .net) to a standard .com address. It can also be used to redirect from an old domain name to a new domain name.

server { listen 80; listen 443 ssl; server_name; return 301 $scheme://$request_uri; }

For most instances, the return command is preferred to the rewrite command.

This process is similar to a standard page redirect:

server { server_name; return 301 $scheme://$request_uri; }

There are several reasons to redirect HTTP traffic to HTTPS. You may need to:

  • Force a more secure, encrypted connection.
  • Keep a page with good SEO ranking, but send its traffic to a new page.
  • Notify and temporarily send traffic to an “under maintenance” page.
  • Permanently send traffic from one website to another, i.e. after a corporate merger.

Now you know how to redirect HTTP to HTTPS in Nginx. By editing the configuration file, you can send traffic from a specific destination to a different site and enforce the use of Nginx SSL certificates. This helps you safely manage changes to your website without disrupting the user experience.



#DevOps — #AWS — #Cloud enthusiast.. Views are my own.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store