Nginx (pronounce engine-x) is a powerful high performance open source web server. It can function as a reverse proxy or a POP3/IMAP proxy. It is the third most popular web server and is well known for its increased performance, ease of use and configuration, stability and minimal use of resources. Therefore, in this article, I will show you how to use Nginx to redirect traffic in different ways.
According to numerous studies Nginx has about 32% market share in the web server market. It runs many renowned websites such as Github, Netflix, CloudFlare, Hulu, Airbnb, Hulu, WordPress and many others.
Redirect in Nginx
The ability to transfer the URL from the website to another address or point based on your criteria is an essential feature of the Nginx web server. A Nginx redirection is simple and easy to set up. Often, users choose to redirect a page that has a good SEO ranking. For example, when switching from a CMS to another platform. This will completely change the composition of your URL. So, to keep your current page with the correct SERP position, you can redirect the old URL to the new page.
Here I will take as an example the creation of two types of Nginx redirection: permanent and temporary.
Nginx temporary and permanent redirection in detail
Temporary redirections are beneficial if the location of one page is to change temporarily from one place to another. The 302 redirect response code is used to designate the temporary move of a page.
If the website is being maintained, temporary redirections are used to inform users that the website is unavailable. Another example is when you make temporary redirects of an incomplete page; you link this page to another point or to the main page:
Visitor-> Page-> The website is being maintained
On the other hand, a permanent Nginx redirect informs the web browser that it must permanently link the old page or domain to a new location or domain. To represent this change, the 301 response code of the redirect is used to designate the permanent displacement of a page. This type of redirection is useful when the user wants to change the domain name and only wants a browser to access it.
For example, when you want to change the domain of your website or create a new page for an old site:
Visitor-> Click on www.olddomain.fr -> Redirected to www. new domain.fr
Page redirections in Nginx
In Nginx, most redirections can be performed using the built-in rewriting function. This is the default feature that is available on a clean Nginx installation and that can form both types of Nginx redirection – i.e. permanent and temporary. In its simplest form, it takes a minimum of two cases, i.e. the old URL and the new URL.
It is simple and easy to redirect pages to a temporary or permanent location on the Nginx web server. In /etc/nginx/sites-enabled/default, you must paste the following code, while changing the variables according to your needs:
`Location chemin... { rewrite ^/ancienneURL$ https://www.domaine.fr/nouvelleURL redirect; }`
/!\ if you use an administration panel like Plesk for example, you will be able to add your redirect directly to your web space.
If you want to redirect the page to another link permanently, simply use "permanent" instead of "direct" in the above command. In the meantime, the path is usually /index.html.
Redirecting a domain by Nginx
To redirect one domain to another, use the command below in the terminal:
`server { listen 80; hostname site.fr www.site.fr; rewrite ^ http://www.site.com$request_uri? permanent; }`
Here, we use two domains. The one we want to redirect – www.site.fr, and the new – www.site.com.
Nginx redirection from HTTPS (SSL)
HTTP and HTTPS use different ports – HTTP port 80 and HTTPS port 443. Using HTTPS is much more useful as it protects you from MITM attacks that may divert your session. Remember that for this method to work, you must have an SSL already configured. Thus, to protect all information sent between you and your visitors, it is advantageous to redirect all requests from HTTP to HTTPS. To do this, you can add this modification to the same file:
`server { listen 80 default_server; server_name _; return 301 https://$host$request_uri; }`
Now, all the default HTTP server traffic is redirected to HTTPS.
Redirected from www to non-www
Often you want your visitors to access the ordinary domain of your web page, for example site.fr instead of www.site.fr. Although there are many options to redirect from www to non-www in Nginx, one of the simplest ways to do so is described below:
`server { server_name www.site.fr; return 301 $scheme://site.fr$request_uri; }`
Conclusion
Nginx is one of the most powerful and easy to use web servers, which allows you to perform temporary and permanent redirections as described above.
You now know how to create Nginx redirects from HTTP to HTTPS, from www or Non-www or vice versa. Make sure you use the right types of redirection, as incorrect redirections will affect your ranking in the searches. Through specific redirections, you can take advantage of your current presence on the web while modifying the structure of the site as needed.