How to Do URL masking in NGINX


how to do url masking in nginx

When you move your web pages or domains, you might need to mask your new URLs under the old ones, without using redirection. Sometimes, you might need to serve some of your pages from a different domain or IP altogether, without changing browser URL. In this case, you can use URL masking for your website. Let us take a look at how to do URL masking in NGINX.

 

How to Do URL Masking in NGINX

 

1. Open NGINX config file

Open NGINX config file in a text editor. You will typically find it at /etc/nginx/nginx.conf


$ sudo vim /etc/nginx/nginx.conf

 

2. URL Masking in NGINX

Let’s say you want to map example.com to a specific IP http://122.122.122.122/path such that when users enter the URL your website loads content from the specified IP without changing URL in browser’s address bar.

In that case, add the following lines to your NGINX config file


server {
 listen 80;
 server_name example.com;
 location / {
  proxy_pass https://122.122.122.122/path;
 }
}

In the above code, we use proxy_pass directive to direct the requests sent to example.com to the specified IP

 

On the other hand, if you want to alias a single URL such as


http:// sub.example.com:81/foo/s?id=(a number)

to


http:// www.example.com/sub/(a number)/

 

then add the following lines to your server config file. We use proxy_buffering directive to alias the URL as required


location /sub {
 rewrite ^/sub/(.*)/ /sub/s?id=$1 break;
 proxy_buffering off;
 proxy_pass http://127.0.0.1:81;
}

 

3. Reload NGINX web server

Reload NGINX server to apply changes.

$ sudo service nginx reload

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!