redirect without changing url in nginx

Nginx : Redirect to Another Domain without Changing URL

Sometimes you may need to redirect one web page to another without changing URL in browser. In this article, we will look at how to change domain without changing URL in NGINX. You can easily do this using proxy_pass directive. Please note, you can also use these steps to redirect one web page to another on same domain, without changing browser URL.


How to Redirect to Another Domain without Changing URL in NGINX

Here are the steps to redirect URL to another domain without changing URL.


1. Open NGINX configuration file

Open terminal and run the following command to open NGINX configuration file.

$ sudo vi /etc/nginx/nginx.conf

Also read : How to Check What User NGINX is Running As


2. Redirect URL to another domain

Let us say you want to redirect requests from website www.example1.com/sample-page to www.example2.com/sample-page, then add the following code to your NGINX configuration file. Replace the domain names as per your requirement.

http {
...
    server {
        listen 80;
        server_name example1.com;
        location /sample-page {
            proxy_pass http://example2.com;
        }
     }
...
}

In the above configuration, we create a server a block for source domain, example1.com, to listen to all incoming requests to this domain. For our page /sample-page, we create a location block and add a proxy_pass directive to pass the request to destination domain example2.com.

If you specify the proxy_pass directive without a URI then it will append the URL stub to destination domain also. In this case example1.com/sample-page => example2.com/sample-page

If you specify the final path in proxy_pass, then NGINX will redirect only to that path. For example, here is how to redirect example1.com/sample-page => example2.com/new-sample

http {
...
    server {
        listen 80;
        server_name example1.com;
        location /sample-page {
            proxy_pass http://example2.com/new-sample;
        }
     }
...
}

In the above case, we have specified the full destination path example2.com/new-sample in proxy_pass directive.

You can also use IP address of destination domain, instead of using domain name, if you want. Here is an example, to redirect to different IP address 54.34.23.21

http {
...
    server {
        listen 80;
        server_name example1.com;
        location /sample-page {
            proxy_pass http://54.34.23.21/new-page;
        }
     }
...
}

In fact you can also use proxy_pass to pass request to servers running on other port numbers such as 8000.

http {
...
    server {
        listen 80;
        server_name example1.com;
        location /sample-page {
            proxy_pass http://54.34.23.21:8000/new-page;
        }
     }
...
}


Redirect URL to Same Domain Without Changing URL

If you want to redirect to same server without changing URL, just use 127.0.0.1 as IP address, or use localhost in proxy_pass.

http {
...
    server {
        listen 80;
        server_name example1.com;
        location /sample-page {
            proxy_pass http://127.0.0.1:8000/new-page;
        }
     }
...
}

If you also want to pass request headers during redirection, then add the following two lines before proxy_pass directive.

proxy_set_header Host $host;     
proxy_set_header X-Real-IP $remote_addr;

Here is the above example modified to use proxy_set_header directive

http {
...
    server {
        listen 80;
        server_name example1.com;
        location /sample-page {
            proxy_set_header Host $host;     
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://54.34.23.21:8000/new-page;
        }
     }
...
}

Also read : How to Configure SSL/TLS passthrough in NGINX


3. Restart NGINX Server

Check NGINX configuration for errors.

$ sudo nginx -t

If you don’t see any error messages, then restart NGINX server to apply changes.

$ sudo systemctl reload nginx

Also read : How to Fix NGINX: Too Many Open Files Error

As you can see, proxy_pass is a really useful and versatile NGINX directive to redirect web pages and URLs to different locations, servers, domains and IP addresses without changing the requested URLs.

Leave a Reply

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