In this tutorial, we will discuss how to write a redirect configuration on Nginx, and also give you some examples that might help you understand it. There is some purpose why you want to redirect an incoming request:
- Force user to use HTTPS instead of HTTP
- Force user to use www domain instead of root domain or vice versa
- Force users to use visit other domains because you change your domain name, or for other reasons.
Let’s see more detail.
3XX series HTTP codes that are related to redirection, but mostly you will use 2 of them, which are
301is used when you want the redirection to be permanent. Quoted from Mozilla.org, they said that the search engines will update their link to the resource, so they will not hit the original URL anymore. rig
302us used when you want the redirection to be temporary. Quoted from Mozilla.org, they said that the search engines will not update their link to the resource, which mean search engine will always hit the original URL and get the
302redirect message each time.
Nginx Redirect Syntax
You can use the
return directive combined with the HTTP code to redirect the incoming request like this
return 3XX url;
3XX is the HTTP code whether it is
url is the URL target of the redirection.
return is used within the
server block like this
. . .
return 302 www.example.com;
To make sure you can use this directive, we will provide you some examples of how to use a redirect in some use cases. let’s check this out.
Redirect HTTP to HTTPS
Redirecting HTTP to HTTPS is a very common use case, it used to force the user to use SSL/TLS to access our website, hence improving security. This is the configuration example
return 301 https://example.com$request_uri;
This server configuration will listen on port 80 (the default port for HTTP), and then permanently redirect it to the HTTPS version of the domain.
$request_uri is used to keep the URI including argument if available, so when it is redirected, the URI will not missing.
Redirect Root Domain to WWW Domain or Vice Versa
Another use case is to redirect a root domain to www domain. for example:
facsiaginsa.com redirected to
www.facsiaginsa.com. I personally don’t like to use the www domain, but if you need it, this is the nginx configuration
return 301 http://www.example.com$request_uri;
if you want the opposite (redirect www domain to root domain), you can use this configuration instead
return 301 http://example.com$request_uri;
Redirect Current Domain to Other Domain
The last use case is redirecting your domain to another domain. It is used when you want to change your domain name. So you want to drive the traffic from the previous domain to the new one. This is the configuration example:
listen 443 ssl;
return 301 $scheme://otherexample.com$request_uri;
In this use case, we listen to both
https and then we will redirect the request accordingly.
$schemecontains the protocol values from the original request URL (
https), so the
http request will be redirected to
https will be redirected to
That’s it! Now you understand how to create a redirect in Nginx.