Last Updated: February 25, 2016
· geetotes

Force www for nginx websites

So I'm still learning nginx, so this may not be a protip, but anyway.

I have domain and I want all requests to re-direct to I'll need to set up two rules in nginx. The first is a simple rule to catch the non-www and redirect it.

server {
rewrite ^(.*)$ $scheme://;

Why no nginx equilavent to httpd's ServerAlias? I don't know. I'm crying: T_T

However, the $scheme built-in variable is hella cool

Now let's set up a rule to catch and we're done
root /var/www/;
#more directives, etc, etc

And the rewrite is complete!