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.