Nginx redirect www to non www domain

January 25, 2012

I've been playing around with Nginx web server over the past few days, its a great light weight web server, ideal for VPS's or smaller Amazon EC2 instances where resources are not as abundant.

One thing I like about nginx so far is the configuration, while I haven't had to do anything overly complex with it yet, it does seam to be quite flexible.

Here's a quick example of redirecting a www domain to the non www version:

server {
   listen 80;
   rewrite ^ permanent;

Note that's just one way of doing it, by creating a new virtual server for the non-www hostname and redirecting all requests. You can also do this from within your main server declaration, eg:

server {
   listen 80
   root /web/root/;
   if ($host != '') {
      rewrite ^ permanent;

I like the first method better, but this just goes to show how flexible the configuration is for nginx

Like this? Follow me ↯

2 people found this page useful, what do you think?


@VBart - very nice example thanks for posting.

Post a Comment


Foundeo Inc.