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

August 12, 2017 706 views
Nginx Ubuntu 16.04

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.

1 Answer

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!

Have another answer? Share your knowledge.