Report this

What is the reason for this report?

Setting up NGNIX to redirect from non-www to www

Posted on August 16, 2016

Can anyone help me set-up the non- www to www redirection?

I 've tried Several tutorials here and not works, my nginx is:

server { listen 80;

server_name mydomain.com;

location / {
    proxy_pass MY_IP_SERVER:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

}



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
0

Accepted Answer

The recommended way to do this is:

server {
    listen       80;
    server_name  example.org;
    return       301 http://www.example.org$request_uri;
}

server {
    listen       80;
    server_name  www.example.org;

    location / {
        proxy_pass MY_IP_SERVER:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

Replace example.org with your domain name.

And if anyone needs it, we’ve built a docker image to do just the non-www-to-www-redirect: https://hub.docker.com/r/webhare/www-redirector/

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.