Nginx www rewrite

Quick tip for rewriting to or vice versa in Nginx.

My conf file originally looked like:

server { server_name; }

I had to create another server block, and move all the conf information to the new block. In my case I was rewriting www to non-www.

server { server_name; //rewrite here; } server { server_name; //all the server stuff goes in this block; }


Instead of mucking around with rewriting, I should have just used return, like so:

server { server_name; return 301$request_uri; }

The lesson is, always check out the official docs before Google.

Comments can be made via the comment box below or by leaving a comment on an issue over at the blog-comments repo with the title of the issue being the path of the post, e.g. writing/ajax-and-rails.