Setup Nginx HTTP / HTTPS and Domain Redirects on Ubuntu 16.04 LTS Servers

Want to learn how to setup Nginx redirects? Migrating or switching domains? The steps below can help…

This post shows you to redirect an old domain to a new one or one sub-domain to another or HTTP to HTTPS, the steps below is a good starting point… For example, with Nginx redirect modules, one can redirect http//example.net to http://example.com or http://www.example.com to http://example.com or http://example.com to https://example.com

This brief tutorial is going to show students and new users how to set up Nginx directs easily… the method below can also be used with switching your HTTP domain to HTTPS…

When you’re ready to configure Nginx directs, continue below:

Step 1: Install / Configure Nginx HTTPS

To configure Nginx to redirect all traffic to HTTPS, you must configure and enable its SSL modules.. The post below shows you how to install and configure Nginx to communicate over HTTPS… however, both HTTP and HTTPS are enabled… the method below will show you how to redirect all traffic to the server to use HTTPS only..

Setup Nginx HTTP Server Self-Signed SSL/TLS Certificates on Ubuntu 16.04 LTS Servers

Step 2: Redirect Nginx HTTP to HTTPS

Our post above set up Nginx to communicate over both HTTP and HTTPS… however, in today’s environment, it’s recommended to choose one type of protocol and redirect the other to it..

For example, you should redirect all HTTP (80) to HTTPS (443). To do that, add the highlighted portion of the code to your Nginx site configuration file…

Run the commands below to open Nginx default site configuration file..

sudo nano /etc/nginx/sites-available/default

Then add the highlighted portion to the bottom of the file.

Redirect to same domain on HTTPS only

server {
  listen 80;
  listen [::]:80;
  server_name example.com www.example.com;
  return 301 https://example.com$request_uri;
 }

server {
    listen 443;
    listen [::]:443;
    root /var/www/html;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    location / {
        try_files $uri $uri/ =404;       
    }

  
     # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
               include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
               fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
}

The configuration above redirect all requests to use HTTPS only to communicate with the server with domain name example.com. So, http://example.com and http://www.example.com will be redirect to https://example.com

Step 3: Redirect Old Domain to New

If you want to redirect from an old domain to a new one, the setting below should help you do that… For this setup, HTTPS isn’t required… Run the commands below to open Nginx default site configuration file.

sudo nano /etc/nginx/sites-available/default

Then add the highlighted portion to the bottom of the file.

Redirect old domain to new domain

server {
  listen 80;
  listen [::]:80;
  server_name example.net www.example.net;
  return 301 http://example.com$request_uri;
 }

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    location / {
        try_files $uri $uri/ =404;       
    }

  
     # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
               include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
               fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
}

The above code added to your Nginx site configuration file will redirect an old domain to a new one without HTTPS…. the highlighted code above redirects http://example.net and http://www.example.net to http://example.com

After making the changes above, run the commands below to test your settings to make sure Nginx is configured correctly..

sudo nginx -t

If you don’t see any error messages then you’re good. Restart nginx  web server by running the commands below.

sudo systemctl restart nginx.service

That’s it! This is how to configure Nginx HTTP 301 redirects

~Enjoy~

You may also like the post below:

Setup Apache2 HTTP / HTTPS and Domain Redirects on Ubuntu 16.04 LTS Servers

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.