Tim321
By:
Tim321

How to redirect all https requests to https www subdomain with Nginx?

January 7, 2017 894 views
Nginx Ubuntu 16.04

I have redirects for http to https working fine. But redirects from https to https does not work. Below is my nginx config file. I would think that the if condition would solve things, but it doesn’t. https://example.com gives the error refused to connect when it should redirect to https://www.example.com. How can I fix this? Note, the ssl cert I'm using does support both www.example.com and example.com. I followed the setup from https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04 if that helps.

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

server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-example.com.conf;
        include snippets/ssl-params.conf;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

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

        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

        location ~ /.well-known {
                allow all;
        }

        location / {
                try_files $uri $uri/ =404;
        }
}
3 Answers

@Tim321

Remove:

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

Change:

server_name _;

To:

server_name example.com www.example.com;

Then restart NGINX and let me know if that works.

    • @Tim321

      Are you still getting refused to connect or another error?

      The best thing to do at this point is check your error logs and see what they are showing. You can do this by checking /var/log/nginx or, in the event the logs are contained in the primary directory, /etc/nginx.

      You can run tail -20 error.log on the file to display the last 20 items logged to the error.log file. That'll help us to see what's really going on and what, if anything else, is actually causing the error(s).

you can also try following.

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

server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-example.com.conf;
        include snippets/ssl-params.conf;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

        location ~ /.well-known {
                allow all;
        }

        location / {
                try_files $uri $uri/ =404;
        }
}

type following just to be sure you dont have problem in any other place.

sudo nginx -t
Have another answer? Share your knowledge.