How to Redirect URLs in Nginx

How to Redirect URLs in Nginx
Nginx is a lightweight web server, which is often used as a reverse proxy, web server, and a load balancer as well. Nginx, by default comes up with a lot of useful features, and more can be added as modules when it’s being installed. This guide intends to demonstrate how to use Nginx to redirect URLs to different directions. Even though Nginx provides plethora of features to redirect URLs, this guide uses a fraction of them as it’s intention is to teach only the essential ones in URL redirection. The areas covered in this guide are redirect unsecure (port 80) URLs to its secured version, redirect a request to the IP to a domain name, and finally redirect any other sub domains, domains to the main domain.

Pre-requirements

First of all, this guide assumes the user has a proper SSH client had installed on the computer, if not go ahead and install Putty as the client, then use the following commands. Additionally, having Nginx, Nano editor are required as well.

  1. Type the following commands to install Nano text editor. The first command helps to retrieve latest packages from the repositories, and the second command installs the latest version of nano text editor.
sudo apt-get update
sudo apt-get install nano
  1. In the terminal window, type the following command to change the current directory to nginx directory.
cd /etc/nginx/sites-available
  1. Now type nano default or the file’s name associated with the domain to change the settings of the domain.
  2. Since now follow one of the following segments to proceed.

Redirect from HTTP (Port 80)

Google, Bing and many other search engines nowadays favor websites with an encrypted connection. When the connection between the client, and server is encrypted, the data transmitting through that particular connection is secure, and thus third parties are unable to access to those data. When the connection is not encrypted, such sites are insecure, and thus it jeopardizes the safety of the data. Insecure website uses port 80 to provide its service to public. Unfortunately, by default the web browser connects with the port 80, as web server assumes it’s what the client wants by default, and thus the request has to be redirected to its secured version. There are multiple ways to get it done with Nginx.

Method 1

If the current domain name is available, and if it receives requests from clients, then they can be redirected to another domain with the following code snippet. Simply copy it to the default file or the file of the domain.

Default server parameter specifies this server block is the default server, hence any requests to the port 80 executes this server block at first by default, and then rest follows thereafter. The parenthesis signifies it also captures requests from ipv6 networks. Return 310 signifies, the redirection is permanent, and thus link juice is passed along with it.

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

Method 2

If the current server has no website attached to it, and the requirement is redirecting any requests to the port 80, then the following server block can be used. Copy it to the default file as stated earlier. Here _ (underscore) signifies any domain. Like earlier, default_server parameter, parenthesis (for IPv6 addresses) like optional attributes can be used here as well.

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

 

Method 3

The following code snippet signifies if the connection is not encrypted, meaning port 80 receiving requests, then they are redirected to a secure version of the specified domain. This should be copied to anywhere in the server {} block, but after the server_name parameter.

if ($scheme != "https") {
return 301 https://$host$request_uri;
}

 

Redirect from The IP Address

Unlike a shared host, both dedicated servers, and virtual private servers always have a dedicated IP address allocated to it. If the web server is configured with Nginx with underscore (which means server processes every request), then any request to the IP address gains access to the website as well. Having access to the website through an IP address is not something every web master wants due to various reasons. On the other hand, if every request is processed, malicious users can associate any random domain with the web server, which is not good for the name of the brand or the business, and therefore it’s important to process only requests to specific domains or and IP address. This segment demonstrates in such cases how to process requests to the IP address of the web server. Using this code block along with one of above code blocks (except method 2 of previous solution) ensures every request to both domain, and IP is redirected to the desired destination.

As said above, copy the following code snippet to default file of Nginx (pre-requirements, 3rd step). Instead of using the name of the domain in server_name parameter, simply use the IP address of the server, then in the next line, use “return 301 domain” to where the request is being redirected. Now when a request to this particular IP address received to the server, it’s redirected to the stated domain. A best example for that is, when a random user types the IP of the web server to access the site directly. If the following code snippet isn’t stated anywhere in default file, any request to the IP isn’t processed; hence users are unable to access the web site via the IP address.

server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name 192.168.1.1;
return 301 https://nucuta.com;
}

Redirect from any other Domain

This solution is same as the first solution of this guide, except it also redirects requests to the 443 port of the web server, meaning both secured, and unsecured requests are redirected to the stated domain in return parameter. As said earlier, simply copy this to the default file.

server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name domain.com www.domain.com;
return 301 https://nucuta.com;
}

 

Finalizing

After following one of the above solutions, nginx file has to be compiled to take its configuration into effect. However, the default file has to be tested before compiling, as it prevents the web server from crashing if there was an error in the configuration.

  1. Simply use the following command in Linux terminal to test the default configuration file, it the result is good continue to the next step.
sudo nginx -t
  1. Use one of the following commands to restart the Nginx web server. The command depends on the name, and version of Linux distro.
sudo systemctl restart nginx
sudo service nginx reload
sudo /etc/init.d/nginx reload
Related Posts
Leave a Reply

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