Question

Unable to get https://www redirecting to non www

Hi all,

I’ve set up an Ubuntu 16.04 VPS with a Jekyll site and LetsEncrypt using tutorials found on this site.

I’d like to get https://www.example.com redirecting to https://example.com however I’m having difficulty getting nginx to co-operate. Where am I messing up?

My current server block (directly after the one that was autocreated using certbot in sites-enabled) looks like:

server {
    listen 80;
    server_name example.com.au;
    return 301 https://example.com.au$request_uri;
}

server {
    listen 80;
    server_name www.example.com.au;
    return 301 https://example.com.au$request_uri;
}

I’ve tried modifying this to listen on port 443. I’ve tried adding these to sites-available. I’ve restarted. But none of my modifications actually make any changes. They dont’t even break the site which would at least tell me if anything I’m doing is having ANY effect.

curl -l www.example.com shows a 401 redirect, to https I presume.

curl-l https://www and non www both bring back the site.

Where am I going wrong?

EDIT: A little further digging

So I ran sudo nginx -t and it returns OK. But then I ran service nginx config test and I got a fail. I then ran sudo nginx -c /etc/nginx/sites-enabled/default -t and got:

nginx: [emerg] “server” directive is not allowed here in /etc/nginx/sites-enabled/deafult:16

But that one is one that was created by certbot? Not sure what’s going on there.


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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

So I think it has something to do with the sites-available file not being included properly in the nginx.conf? But I’m not sure. I’m lost!