This is the 8th day of my participation in Gwen Challenge.
How to Use Nginx Redirect Nginx (pronounced engine-x) is a powerful open source high-performance HTTP Web server. It can be used as a reverse proxy or POP3/IMAP proxy. It is the third most popular Web server, known for its enhanced performance, ease of use and configuration, stability, and minimal resource utilization. That’s why, in this tutorial, we’ll show you how to redirect traffic in different ways using Nginx.
Redirection in Nginx the ability to forward a Web site’s URL to another address or point based on your criteria is a basic feature of the Nginx Web server. Nginx redirection is simple and easy to set up. Users often choose to redirect pages with good SEO rankings. For example, when switching from CMS to another platform. This will completely change your URL composition. Therefore, to keep the current page in a good SERP position, you can reroute the old URL to the new page.
In this tutorial, we will guide you through creating two types of Nginx redirection: permanent and temporary. Remember to install Nginx on your VPS.
Temporary and Permanent Nginx redirects explain that temporary redirects are beneficial if the page location needs to be changed temporarily from one location to another. The redirect response code 302 is used to specify the temporary movement of the page.
If site maintenance is being performed, a temporary redirect is used to notify the user that the site is unavailable. Another example is when you temporarily redirect an incomplete page; You link the page to another point or home page:
Visitor -> site page -> site maintenance
Permanent Nginx redirects, on the other hand, inform the Web browser that it should permanently link the old page or domain to the new location or domain. To map this change, the redirect response code 301 is used to specify the permanent movement of the page. These types of redirects are useful when the user wants to change the domain name and no longer wants the browser to access it.
Page redirection in Nginx Remember that you must first access your VPS over SSH. If you run into problems, check out our PuTTY tutorial.
In Nginx, most redirects can be done with built-in override functionality. This is the default feature available for a new Nginx installation and can form two Nginx redirects — permanent and temporary. In its ordinary form, it requires at least two cases, the old URL and the new URL.
Redirecting pages to temporary or permanent locations on the Nginx Web server is simple. In the location /etc/nginx/sites-enabled /default you should paste the following code while changing the variable to suit your needs:
Location path_pattern {override ^/oldURL$www.domainone.com/newURL redirect; }
If you want to permanently redirect a page to another link, simply use “permanent” instead of “redirect” in the command above. Also, path_pattern is usually /index.html.
Nginx Redirection domain To redirect one domain to another, use the following command on the terminal:
Server {listen to 80; Devisers. In www.devisers.in; Rewrite ^ www.devisers.com$request_uri? The eternal; }
In this case, we use two fields. The one we want to redirect – www.devisers.in, and the new one – www.devisers.com.
Nginx redirects from HTTP to HTTPS (SSL) HTTP and HTTPS use different ports — HTTP port 80 and HTTPS port 443. Using HTTPS is more helpful because it protects you from MITM attacks that can hijack sessions. Remember that you need SSL set up for this method to work. Therefore, in order to protect all information sent between you and your visitors, it is beneficial to redirect all requests from HTTP to HTTPS. To do this, we can add this change to the same file:
Server {listen to 80 default_server; Server name _; 301 https:// hosthosthostrequest_uri; } Now all traffic from the HTTP default server is redirected to HTTPS.
Nginx redirects specific sites this is important if you use a variety of sites or applications and only want to redirect a single site. Please follow the following steps:
Server {listen to 80; server_name devisers.in; Return 301 devisers. In $request_uri; } Here, we redirect the site www.devisers.in to www.devisers.in
Redirects from WWW to non-WWW Many times, you want visitors to come to a common field of your web page, such as devisers.in instead of www.devisers.in. Although there are many options in Nginx to redirect from WWW to non-WWW, one simple way to do this is described below:
Server {server_name www.devisers.in; Returns a 301 scheme: / / devisers. Inscheme: / / devisers. Inscheme: / / devisers. Inrequest_uri; } Important: This is a permanent redirect or “301 redirect”.
Restart the Nginx Web server for the changes to take effect using the following command:
If you wish to redirect from non-WWW to WWW, simply replace the website URL mentioned in the command above. Replace www.devisers.in with devisers. In and vice versa.
conclusion
Nginx is one of the most powerful and easy-to-use Web servers that allows you to do both temporary and permanent redirects as described above.
Now you know how to create Nginx redirects from HTTP to HTTPS, from WWW or non-WWW, or vice versa. Make sure you use the correct type of redirection, because incorrect redirection can affect your search ranking. With the help of accurate redirection, you can take advantage of the current Web state while changing the site structure as needed.
We hope you found this tutorial helpful! See you in the next post.