How to set up an internal redirect in Nginx


TL;DR for how to set up and internal redirect in nginx:

location ~ /security-in-remote-working-culture/ {
                proxy_cache my_zone;
                proxy_pass http://bluegrid.io;
                proxy_cache_valid 200 1d;
                add_header Cache $upstream_cache_status;
                proxy_cache_min_uses 2;
 
                rewrite ^(.*)$ /vpn-options-for-it-nomads/ last;
 }
Specification:

OS Version: Ubuntu Os 18.04.3 (LTS) x64
Nginx version: nginx/1.14.0

Full read:

The most common redirection we all know is when we send a requester from one location to another. 301 or Permanent Redirection and 302 Temporary Redirection are the two most commonly used. 

Both are sending the request to a new location. 301 should be used only if you are confident that search engines should index the new location in place of the old ones. 302 is typically used when we want to serve content from another location only temporarily. For example, if we are having maintenance on the website and we need to serve the Under Maintenance page. 302 will tell search engines not to index this page as it’s only temporarily there.

30X rewrite diagram
30X rewrite diagram

Now, let’s see when we need internal redirection. A typical example is when we have a new page that should serve the new content in place of the old one. We can’t just use the new URL to a new page because the old one has a good SEO ranking and is already popular so, we want to use its rating:

Internal rewrite diagram
Internal rewrite diagram

In this example, we’ll use /security-in-remote-working-culture/ as the old page. And then /vpn-options-for-it-nomads/ as a new one. We’ll be serving the content from the new one in the URL of the old one.

Let’s make sure we have some identification of the old page (Etag, content-length…):

bluegrid-edu:~# curl -I http://bluegrid.io/security-in-remote-working-culture/
HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 26 Jul 2020 14:13:55 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.38
Last-Modified: Sun, 26 Jul 2020 14:13:53 GMT
ETag: "93f69114682172bc71948b568fc1ef7a"
Vary: Accept-Encoding
Referrer-Policy: no-referrer-when-downgrade
Cache: HIT

Now, let’s create an internal redirection section:

location ~ /security-in-remote-working-culture/ {
                proxy_cache my_zone;
                proxy_pass http://bluegrid.io;
                proxy_cache_valid 200 1d;
                add_header Cache $upstream_cache_status;
                proxy_cache_min_uses 2;
 
                rewrite ^(.*)$ /vpn-options-for-it-nomads/ last;
 }
 
location / {
                proxy_cache my_zone;
                proxy_pass http://bluegrid.io;
                proxy_cache_valid 200 1d;
                add_header Cache $upstream_cache_status;
                proxy_cache_min_uses 2;
}

Note that we have created a new location block that would match the request for the old page. Then we had to apply all the rules we had under location / block. In this new block, we are performing the redirection and the result is as follows:

bluegrid-edu:~# curl -I http://bluegrid.io/security-in-remote-working-culture/
HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 26 Jul 2020 14:14:50 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.38
Last-Modified: Sun, 26 Jul 2020 14:14:50 GMT
ETag: "86869b3a9bf15cac8e572ac8f1a25dab"
Vary: Accept-Encoding
Referrer-Policy: no-referrer-when-downgrade
Cache: MISS

How do we know it works – The cache identifier is Cache: MISS and the ETag value is changed. You can test all of our examples by opening the page in the browser, however, for the sake of readability, we are using headers as the method of testing.

If you open the old URL in your browser you’d see that the content is now what is located on a new one, without being redirected to the new page.

Schedule a consultation:

    Share this post

    Share this link via

    Or copy link