9

Let's say I want to redirect from www.example.com to example.com and I want to do this using nginx. I looked around and did not see any good documentation on this so I figured I would ask and answer my own question.

5 Answers 5

7

I also looked at this on the Nginx wiki and other blogs and the best way performance wise is to do the following:

To redirect from www.example.com to example.com using nginx (version 1.0.12 at time of writing).

server {
  server_name www.example.com;
  rewrite ^ $scheme://example.com$request_uri permanent; 
  # permanent sends a 301 redirect whereas redirect sends a 302 temporary redirect
  # $scheme uses http or https accordingly
}

server {
  server_name example.com;
  # the rest of your config goes here
}

When requests come to example.com, no if statements are used for performance. And it uses $request_uri rather than having to create a $1 match which taxes the rewrite (see Nginx Common Pitfalls page).

Sources:

1
  • In my test, the directives must be reversed, ie. the rewrite one must come after the main server {} configuration block.
    – karmi
    Jul 16, 2012 at 13:17
5

Please visit this question in SO: https://stackoverflow.com/a/11733363/846634

From the better answer:

Actually you don't even need a rewrite.

server {
    #listen 80 is default
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    #listen 80 is default
    server_name example.com;
    ## here goes the rest of your conf...
}

As my answer is getting more and more up votes but the above as well. You should never use a rewrite in this context. Why? Because nginx has to process and start a search. If you use return (which should be available in any nginx version) it directly stops execution. This is preferred in any context.

4

After some digging around and some missteps, here is the solution. The gotcha I ran into is to make sure to use "http://example.com$uri". Inserting a / in front of $uri results in a redirect to http://example.com//

  server {
    listen 80;
    server_name www.example.com;
    rewrite ^ http://example.com$uri permanent;
  }

  # the server directive is nginx's virtual host directive.
  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 80;

    # Set the charset
    charset utf-8;

    # Set the max size for file uploads to 10Mb
    client_max_body_size 10M;

    # sets the domain[s] that this vhost server requests for
    server_name example.com;

    # doc root
    root /var/www/example.com;

    # vhost specific access log
    access_log  /var/log/nginx_access.log  main;


    # set vary to off to avoid duplicate headers
    gzip off;
    gzip_vary off;


    # Set image format types to expire in a very long time
    location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
        access_log off;
        expires max;
    }

    # Set css and js to expire in a very long time
    location ~* ^.+\.(css|js)$ {
        access_log off;
        expires max;
    }

    # Catchall for everything else
    location / {
      root /var/www/example.com;
      access_log off;

      index index.html;
      expires 1d;

      if (-f $request_filename) {
        break;
      }
    }
  }
1

To redirect to non-www, amend vhost file:

server {
  listen 80;
  server_name www.example.com;
  rewrite ^/(.*) http://example.com/$1 permanent;
}

'Permanent' turns the redirect into a 301 redirection. After this code block, you can config the domain without www.

For redirecting non-www to www:

server {
  listen 80;
  server_name example.com;
  rewrite ^/(.*) http://www.example.com/$1 permanent;
}

Thassit.

BTW, for full VPS setup using Nginx, check out the VPS Bible on my site, guvnr.com, and I hope that's handy!

0

This is what I use:

# ---------------------------------------
# vHost www.example.com
# ---------------------------------------

server {

##
# Redirect www.domain.tld
##

    server_name  www.example.com;
    rewrite ^(.*) http://example.com$1 permanent;

}

# ---------------------------------------
# vHost example.com
# ---------------------------------------

server {

   # Something Something
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .