0

This question already has an answer here:

I have a react website served by reverse proxy inside nginx. On top of that, I have SSL. Certbot is handling redirects from http to https. I would also like to setup redirects from www to non-www. I am wondering what is the correct way to do this?

My config looks like this:

server {
    server_name example.com www.example.com;
    location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto http;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Host $remote_addr;
        proxy_cache_bypass $http_upgrade;
        proxy_hide_header X-Powered-By;
        add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-XSS-Protection "1; mode=block" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "origin-when-cross-origin" always;
    }
    error_page 502 /index.html;
    location /index.html {
        root /var/www/subdomains/service;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.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 = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = www.example.com) {
        return 301 https://example.com$request_uri;
    }

    server_name example.com;
    listen 80;
    return 404; # managed by Certbot
}

Note that both of the pages (www and non-www) are currently working. I just want to avoid duplicate links, so I would only like the non-www version.

marked as duplicate by bodgit, womble Dec 18 '18 at 8:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Split your SSL server block in two:

server {
    server_name www.example.com;
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.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
    return 301 https://example.com$request_uri;
}
server {
    server_name example.com;
    ... # your main config here
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.