Nginx

How to do Redirect in Nginx

redirect-in-nginx
mm
Written by Santosh Prasad

Nginx is a free, open source, high-performance and extremely efficient and quite flexible HTTP web server and reverse proxy, as well as an IMAP/POP3 proxy server. When you want to do a redirect in Nginx, you have a few options to select from, so you can choose the one that suits you best to do an Nginx redirect.

Simple and Fast : Return

The by far simplest and fastest – because there is no regexp that has to be evaluated – is to use the return statement. Put this in your server block:

return 301 https://example.com$request_uri;

In above example this is 301 permanent redirect. You can use 302 if you want a temporary redirect. A full sample server block could be:

server {
    listen 80;
    listen [::]:80;
    hostname example.com www.example.com;
    return 301 https://example.com$request_uri;
}

Regular Expressions

You can use a regexp if you need something more complex, this is still extremely fast in Nginx:

rewrite ^/foo/(bar)/(.*)$ https://$server_name/$1/$2 permanent;

Replace permanent with redirect if you want a temporary (HTTP 302) redirect. Our full sample server block could then be:

server {
    listen 80;
    listen [::]:80;
    hostname example.com www.example.com;
    root /var/www/example.com/public;
    rewrite ^/foo/(bar)/(.*)$ $scheme://$server_name/$1/$2 permanent;
}

Using Maps

If you have a list of URLs or regular expressions that you want to redirect differently, you can use a map, which you very well may define in a separate file for your convenience. Just note that the map definition must be outside the server block:

include redirect-map.conf;
server {
    […]
    if ( $redirect_uri ) {
        return 301 $redirect_uri;
    }
}

Your redirect-map.conf file look as shown below:

map $request_uri $redirect_uri {
    /about.html          /about-us;
    /customers.html      /our-customers;
    /products.html       /our-products;
}

Examples:

map $request_uri $redirect_uri {
    /about.html          /about-us;
    /customers.html      /our-customers;
    /products.html       /our-products;
    # Match any url that ends in products.html or producs.htm
    ~products\.html?$    /our-products;
    # case-insensitive version of the above
    ~*products\.html?$   /our-products;
    # A named capture that maps
    # e.g. product-1234.html into /products/item-1234/overview
    ~product-(?\d+)\.html   /products/item-$sku/overview;
}

Some Variables

I’ve used a few of these in the examples above, so you might have noticed them before. These are variables that comes predefined by Nginx, ready for you to use in your configs:
$scheme – The scheme used for the current request. E.g. “http” or “https”
$host – The hostname provided by the client for the current request.
$server_name – The first hostname from the hostname declaration in your config for the server block that responds to the request.
$request_uri – The full original request URI – with arguments.
$request_filename – The file path for the current request.

Redirect HTTP to HTTPS

Redirect all HTTP traffic to HTTPS in your Nginx config:

server {
	listen 80 default_server;
	listen [::]:80 default_server;
	server_name _;
	return 301 https://$host$request_uri;
}

Now all traffic for http://example.com/foobar is redirected to https://example.com/foobar. Please note that while this works fine for GET requests, the postdata is not sent to the new URL for POST requests. This is usually not an issue if you’re using WordPress – at least not if your website is coded somewhat properly – as all your forms should use the URL WordPress is configured to use.

Redirect Old Domain Name To New Domain Name

Edit the nginx configuration file /etc/nginx/nginx.conf.

Now add the below line in server {….} block location after server_name directive:

# vim /etc/nginx/nginx.canf 

rewrite ^ $scheme://www.bla.com permanent;

With the above redirect your all file and folder will redirect to http://www.bla.com permanently.

Sample Code

### redirect bala.bala.com$URI to www.bla.com$URI with 301 ###
  server {
    listen      192.168.0.5:80;
    server_name bala.bala.com;
    root        /var/www/html/;
    index       index.html;
    rewrite     ^ $scheme://www.bla.com$request_uri permanent;
    # .... 
 }

Force Redirect Non-WWW To WWW Domain

Edit the nginx.conf file to force non-www to www domain.

# vim /etc/nginx/nginx.conf

### redirect non-www to www with client code 301 ###
      if ($host = 'looklinux.com' ) {
         rewrite  ^/(.*)$  http://www.looklinux.com/$1  permanent;
      }
Thank you! for visiting Look Linux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment