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

I am trying to 301 redirect to without any extreme modifications to the nginx setup in /etc/nginx/sites-enabled/, 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/;
        index index.html index.htm index.nginx-debian.html;

    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/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # 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 = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

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

        listen 80;
        listen [::]:80;

    return 404; # managed by Certbot


Things I have tried:

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

 if ($host = {
        return 301$request_uri;
    } # managed by Certbot

Also have tried adding a new server block for servername, but neither of these seemed to redirect to 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.

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
February 23, 2020
Accepted Answer

Hi there @jimmydeal16,

Have you tried following the steps from this tutorial here:

Basically it suggests creating a separate server block and using the following

server {
    return 301 $scheme://$request_uri;

What you currently have in your port 80 server block is good enough, in your case as you have an SSL certificate, you might have to add one more server block for https://www redirect to https:// only:

server {
    listen              443 ssl;
    ssl_certificate     /path/to/certificate.crt;
    ssl_certificate_key /path/to/private/key.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    return              301$request_uri;

Hope that this helps! Let me know how it goes. Regards, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel