NGINX www and non-www url standardization/canonicalization

April 24, 2013 4.2k views
I can't seem to get url standardization to work with nginx, the service restarts fine, so i assume the code is sound just not doing what i want it to do. Does this look right for the server blocks? I've also tried return instead of rewrite. server { listen 80; server_name www.example.com; rewrite ^ $scheme://example.com$request_uri permanent; } server { listen 80; server_name example.com; root /usr/share/nginx/www/example.com; index index.php index.html index.htm; etc...
4 Answers
RESOLVED
i was stupid, it was a dns issue, for anyone else having this issue, you should check if your www. hostname is resolving at all, if it's not, dns is probably not set up properly (specifically a c-name record), then to get the rest of the url's to redirect, the server blocks with the redirect "command?" in it should take care of it (for later versions of nginx, earlier may require a "rewrite")
This is not the place for this.
ok, sorry, can you remove it then
# Your 301 redirect www to non-www
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}

# Your Server
server {
listen 80;
server_name example.com;
root /link/to/your/website/root;
index index.php index.html index.htm;
....etc
}
Have another answer? Share your knowledge.