| by Arround The Web | No comments

Redirect URLs in NGINX

NGINX is a slightly elevated, fully accessible, free IMAP/POP3 VPN, HTTP web service, and reverse VPN connection. NGINX attributes are good speed, reliability, functionality customization, easy setup, and low resource consumption. It has many features, and many more might be added as plugins during installation. The topics discussed in this article are changing insecure (port 80) URLs to encrypted versions, changing an IP request to a DNS server, and changing any other domains to the main domain.

Prerequisites for NGINX

The guidance implies that the user has a valid SSH client installed on their computer. The following are the commands for the default software NGINX installation from a Linux Repository.

Step 1: Changes to the Repository

Initially, what you have to do is to update your system using the affixed command. You will get similar output as shown in the following image.

$ sudo apt-get update

 

Step 2: Installing NGINX

Once you are done with the updates, install the NGINX in your Ubuntu 20.04 by utilizing the following appended instructions on the terminal shell of Ubuntu 20.04.

$ sudo apt install nginx

 

Step 3: Validation of NGINX

Let’s execute the following instruction to validate which version of Nginx is installed on your system using the “- v” flag:

$ sudo nginx -v

 

Redirecting URLs in NGINX

A URL on your site must be redirected to a new URL if you browse a page. In NGINX, there are several ways to redirect a URL. Let’s examine each one in turn.

Temporary NGINX Redirect

If a page’s URL needs to change temporarily from one URL to another, temporary redirects are helpful. The quick-moving of a page is indicated by the redirect response code 302. Temporary redirects inform users that a site is inaccessible while maintenance is conducted.

User->Page of the website-> The site is still being updated.


The browser is supposed to send all queries from “www.nginxurl1.com” to “www.nginxurl2.com” through this redirect. This technique cannot fix the entire site; only a single page can be. You can specify entire directories rather than individual files when using the rewrite directive with regular expressions to redirect more than one page.

Permanent NGINX Redirect

A permanent NGINX redirect notifies the web browser that it should link the previous page to the new URL permanently. The permanent relocation of a page is signaled by the redirect response code 301, which is used to map this change. When a user decides to update the URL and no longer wants a website to visit, these types of redirections can be helpful.

User–> To be redirected to www.nginxurl2.com, click www.nginxurl1.com.


Using a rewrite directive, we added the mentioned redirect. Every URL component after the / is matchable by the regular expression /$. One such example is the redirect of www.nginxurl1.com to www.nginxurl2.com. Adding permanent after the rewrite directive is all that is necessary to create a permanent redirect.

Permanent HTTP to non-HTTP Redirection in NGINX

You often want your website’s users to access www.nginxurl1.com/$1 rather than www.nginxurl1.com. Although there are various ways to redirect from HTTP to non-HTTP in NGINX, the following is one of the simplest:

Permanent Redirect HTTP to HTTP in NGINX

HTTP port 80 is used by HTTP and HTTPS. Since MITM(a man-in-the-middle) attacks that can steal your information are prevented by using HTTPS, it is considerably more beneficial to do so. Remember that for this strategy to work, SSL must already be configured. Therefore, rerouting all HTTP queries to HTTPS is advantageous to secure the data transmitted between you and the users.

Add the following HTTP line to your configuration file if you intend to reroute an HTTP to its HTTP form permanently.


Let’s analyze the script previously given:

listen 80: If the provided URL is given, the server block will listen on port 80 for connection requests.

server name: nginxurl1.com, is the host address. www.nginxurl1.com – Indicates the server block’s host names.

return 301:  https://nginxurl1.com, traffic will be forwarded to the website’s HTTPS version using $request uri. The entire original query URI and the parameters are contained in the $request URI field.

NGINX URL Redirect to a Specific Site

If any websites hosted on the site are set up to utilize HTTPS, and you don’t want to install individual HTTP blocks for each site, you can establish a unified catch-all HTTP response block. This element will send all HTTPS queries to the relevant HTTPS elements.

Modify the NGINX configuration file and apply the following modifications to generate a single catch-all HTTPs block that will lead users to the HTTPs version of the website.


Let’s examine each piece of code in turn:

listen 80 default_server: This server block is designated as the default block for any URLs that listen 80 default server does not match.

server_name _: An incorrect website address that never corresponds to an actual web address is server name_.

return 301 https://nginx1$request_uri: Information should be forwarded to the associated HTTPS server block with the response message 301. (Moved Permanently). The web address of the query is stored in the “nginxurl1” variable.

Syntax Checking and Restarting NGINX

The script must be created to put the configurations of the NGINX script into action after implementing each of the fixes. The standard file must be verified during compilation, as it guards against the webpage terminating in the event of a setup failure.

To check the default parameter file, run the following script on a Linux terminal. You can move on to the next stage if everything works as expected.

$ sudo nginx -t

 

To reboot the NGINX web service, utilize any of the commands. The command will vary depending on the Linux distribution’s hostname and edition.


There you go! Now, websites will be redirected to their new destination via the NGINX host.

Conclusion

NGINX enables you to perform the above-described temporary and permanent redirection among the most capable and user-friendly web browsers. Use the appropriate redirections; otherwise, your engine ratings will suffer. You can make the most of your existing internet persona while modifying the layout plan as needed with specific redirection. We have added all the basic steps to redirect URLs in NGINX.

Share Button

Source: linuxhint.com

Leave a Reply