Report this

What is the reason for this report?

How to 301 redirect from www to non-www using nginx as reverse-proxy for node.js app?

Posted on February 21, 2020

I am trying to 301 redirect https://www.tankienews.com to https://tankienews.com without any extreme modifications to the nginx setup in /etc/nginx/sites-enabled/tankienews.com, but have been unable to get the redirect working.

Wasn’t trying to rewrite the whole file as I don’t do too much work on the actual nginx side, so don’t want to risk breaking the site.

I have the domain info setup correctly on digital ocean as I can access both www and non-www version of my site.

Current server setup is:

server {

        root /var/www/tankienews.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name tankienews.com www.tankienews.com;
    location / {
        proxy_pass http://localhost:3000;
        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;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/tankienews.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/tankienews.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = www.tankienews.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = tankienews.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name tankienews.com www.tankienews.com;
    return 404; # managed by Certbot




}

Things I have tried:

Changing the return 301 info for if($host = www.tankienews.com) to the host name for the non-www version as such:

 if ($host = www.tankienews.com) {
        return 301 https://tankienews.com$request_uri;
    } # managed by Certbot

Also have tried adding a new server block for servername www.tankienews.com, but neither of these seemed to redirect www.tankienews.com to tankienews.com when I restarted nginx and then tested in my browser.

Sorry if this is a simple question, but I’ve searched the tutorials (solution in the tutorial for Ubuntu 14.04 didn’t work when I tested it) and the solutions for similar questions also didn’t work when I tested. It’s possible I’m not implementing these solutions correctly, but I followed the instructions in the responses.

Please let me know what the simplest fix to get this working would be. I will continue testing alternative methods and update if I find a solution.

The developer cloud

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

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.