Managing Redirects with Nginx
Nginx

Introduction

Redirect is a function of the webserver which redirects traffic from one URL to another URL. Nginx is one of the most powerful reverse proxy tool for managing and redirecting web traffic. Redirection is an important feature when the requirement arises. 

Using Nginx, the configuration is easy to redirect unencrypted HTTP server to an encrypted HTTPS server. HTTP redirection is also a way to point one domain to another.

The Nginx configuration file i.e. .conf file store inside the document root directory of your site e.g. /etc/nginx/sites-available/directoryName.conf. The document root directory is found where your sites’ files live i.e. /var/www/html. And if the server has multiple sites then it can be /domain.com.

You can copy the default configuration file of Nginx located at /etc/nginx/sites-available/ directory and use this file to append your redirects. Or you can create a new file e.g. domain.com.conf.

Note: In this document, we’re using zehncloud.com as a domain name. Please replace the zehncloud.com with your domain name.

Prerequisites

  • Linux server should be up and running.
  • A non-root user with sudo privileges.
  • Nginx should be installed and running on this server.

Managing Redirects using Nginx

There are different types of redirects which will be discussed below:

  • Redirect from HTTP to HTTPS
  • Create Temporary and Permanent Redirects
  • Redirect from non-www to www

Redirect from HTTP to HTTPS

Load the site using https over http by forcing the browser. It can be used to verify the business ID number and location. If the URL is running on HTTPS protocol, then it helps prevent visiting malicious sites that may cause harm to your private information or personal computer. 
It can be done by forcing https so that all the requests using port 80 for their domain e.g., https://www.zehncloud.com will be redirected to https://www.zehncloud.com

  • To achieve this with NGINX, we will use the return directive of NGINX.
  • Test your configuration syntax with below command

Restart the Nginx using deamon or simple restart with the command

Create Temporary and Permanent Redirects

Temporary and Permanent Redirects are the most common types of redirects.

When a URL needs to served from a different location for a short period or temporarily, then Temporary redirects are useful, and their response status code is 302 Found. This can be used when e.g., you are performing site maintenance, and it needs to use temporary redirects from your domain to any other page, which informs your visitors that the site is in maintenance and will be back soon.

Permanent redirect can take place by informing the browser that should not attempt to access the old address and forget it altogether.
The response status code of permanent redirects is 301 Moved Permanently. This can be used when you want to change the domain name or when your content has been moved permanently on a new location.

Open the server configuration file and add below lines into the server block to create a temporary redirect in Nginx.

Similarly, add below line for permanent redirect:

There is a default directive available on a fresh installation of Nginx, which can be used to create both temporary and permanent redirects.
This built-in rewrite directive takes two arguments that is an old URL and a new URL.
Add below lines for temporary redirection with a rewrite in your server configuration:​​​​​

The solution is only for the single page instead of an entire site.
And for redirecting the entire site, use the rewrite directive with the regular expressions.
For example, if you wanted to redirect every page temporarily within www.zehncloud1.com to www.zehncloud2.com, you could use the following:

​​​​​For ​​Temporary redirect with rewrite:

Replace the “redirect” with permanent at the end of the directive in case if you want to create a permanent redirect. As by default, this rewrite directive creates a temporary redirect.

For Permanent redirects

Redirect from hostname.com to www.hostname.com

Adding the www to a URL when a client requests content from your server can help certain sites (those hosted on WordPress) to function more efficiently. A common .htaccess rule to accomplish this rewrite is:

Create a server block in nginx.conf to accomplish the same task as .htaccess rewrite rule as I mentioned above that it is the best practice to use the return directive if possible.

In this section, there are two server blocks, first listens on port 80 for the request to zehncloud.com which is defined with brackets {}, and then to return a 301 “redirection” to e.g. www.zehncloud.com.

Test the syntax by running below command:

Reload the configuration:

Thank you!

Related Posts