Nginx: 301 Redirect To A Domain Name

See all Nginx webserver related FAQ
I am a new Nginx web server user. How do I redirect to a different domain using nginx (say to permanently?

You need to use HttpRewriteModule module to change URI using regular expressions (PCRE), and to redirect and select configuration depending on variables.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Nginx
Est. reading time 2 minutes

rewrite directive

This directive changes URI in accordance with the regular expression and the replacement string. Directives are carried out in order of appearance in the configuration file. You can use this directive in the following context:

  • server
  • if
  • location

Nginx 301 rewrite syntax

The syntax is:

rewrite regex replacement [ flag ]

Flags can be any of the following:

  1. last – completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.
  2. break – completes processing of current rewrite directives and non-rewrite processing continues within the current location block only.
  3. redirect – returns temporary redirect with code 302; it is used if the substituting line begins with http://
  4. permanent – returns permanent redirect with code 301

Example: Redirect to a different domain with nginx

In this example, redirect all traffic as follows:

From: To:

Edit nginx.conf or your virtual host config and update server context as follows:

server {
  rewrite ^$request_uri? permanent;
  # rest of config, if any goes below ... #

Finally, restart/reload your nginx server, type:
# nginx -t && nginx -s reload

How do I test and verify HTTP 301 redirect?

You need to use curl command:
$ curl -I
$ curl -I

Sample outputs:

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 18 Nov 2013 12:29:08 GMT
Connection: keep-alive
Keep-Alive: timeout=60
Vary: Accept-Encoding
X-Galaxy: Andromeda-2

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

6 comments… add one
  • Alexander Kuznetcov Nov 19, 2013 @ 11:41

    Normally, you should use separate server block with ‘return’ directive, instead of ‘rewrite’ directive inside of main server block, for redirects

    server {
      return 301 $scheme://$request_uri;
    server {
      # rest of config, if any goes below ... #
  • Bob Nov 30, 2013 @ 11:13

    Excellent was looking for this tutorial.

  • mark Apr 26, 2014 @ 7:26

    hi how can i forward any domain comming in (+subdomain) on http to https ?
    so i don’t have to redirect for every singel domain
    i will catch the https later in to different configs but i would only need one port 80 config since they all do the same

  • Suseelan john Dec 4, 2015 @ 10:51

    Sir how can i achieve 301 redirect using htaccess and also enable gzip compression

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.