Tag Archives: nginx

Nginx: redirect non-www to www address‏

Problem (example):
I want to redirect all user request whether it is from http://mp32u.net or http://www.mp32u.net to http://www.mp32u.net . (This method usually used as one of SEO effort).
Nginx didn’t know .htaccess

nginx has it’s own rewrite module and method.

This is an example server block that will solve the problem:

    server {
        listen 80;
        server_name mp32u.net www.mp32u.net;

        # this is the nginx redirection from non-www to www example
        if ($host = 'mp32u.net') {
            rewrite ^(.*) http://www.mp32u.net/$1 permanent;

        # bla bla bla bla ... other settings such as root, index, location.. etc.

As you can see, those server block will handle request that has mp32u.net and www.mp32u.net in its HTTP_HOST.
The if block will check if the HTTP_HOST is exact match to ‘mp32u.net’, if it’s true then user will be redirected to ‘www.mp32u.net’ and the same server block will handle this request. Quite simple isn’t it?

Notes: Of course, now, mp32u.net did not use nginx as it’s web server after I noticed the server load is sky high when using nginx compared to when using apache2