Report this

What is the reason for this report?

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

Posted on August 12, 2017
mdn

By mdn

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.



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.

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!

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.