4 messages in ru.sysoev.nginxRe: redirecting non www to www.
FromSent OnAttachments
iane...@public.gmane.orgJan 14, 2007 3:11 pm 
Jonathan DanceJan 14, 2007 5:53 pm 
iane...@public.gmane.orgJan 14, 2007 6:26 pm 
Igor SysoevJan 14, 2007 9:27 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: redirecting non www to www.Actions...
From:Igor Sysoev (is-G@public.gmane.org)
Date:Jan 14, 2007 9:27:40 pm
List:ru.sysoev.nginx

On Sun, 14 Jan 2007 ianevans-H/XkN3sKs41m/Kok3h1/6Q@public.gmane.org wrote:

Just trying to clean things up for Google's duplicate issues...this might be a basic example to toss in the rewrite module part of the wiki for people who suck at regex's like me. <g>

So a) who would you redirect a example.com request to www.example.com and b) where would it go in the order of the locations:

location / { ... }

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|mov)$ { ... }

You may set the "rewrite" at server level:

server { server_name www.example.com example.com;

if ($http_host ~* ^example.com$ { rewrite ^(.*) http://www.example.com$1 permanent; }

location / { ... }

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|mov)$ { ... }

however, how it was already suggested it's better to separate servers:

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

server { server_name www.example.com; ...

because

1) it's clearer 2) and the frequent requests to www.example.com would not run unnecessary "if" and regex. 3) nginx is optimized to run tens thousands of virtual host names.