bsmith4
bsmith4

Reputation: 97

Nginx HTTPS only works after page is refreshed

I have set up letsencrypt for https and it works fine when there is no www. For some reason I can only get 'example.com' to work fine with https (ie. redirect to https://example.com) but when I go to 'www.example.com' it doesn't go straight to https, only after I refresh the page it does so. Heres my nginx default conf:

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

server {
    # listen 80 default_server;
    # listen [::]:80 default_server;
    listen 443 ssl;
    server_name example.com www.example.com;

    root /var/www/html;

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

    server_name _;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }



    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
}

I've tried all sorts of redirects in the conf but none of them seem to be working. So the https seems to be working but only after a page refresh. Any help would be appreciated, thanks.

Upvotes: 1

Views: 604

Answers (2)

Dileep Jayasundara
Dileep Jayasundara

Reputation: 312

I have noticed you have use two entries for server name. I want to know what was the purpose. Please try this configuration.

  server {
        listen      80;
        server_name www.example.com example.com;
        rewrite ^ https://$host$request_uri? permanent;
    }

    server {
        listen 443;
        server_name example.com www.example.com;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        ssl on;
        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
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
        ssl_session_cache shared:SSL:2m;    


    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
}

Upvotes: 1

bsmith4
bsmith4

Reputation: 97

I have no idea why this worked and other answers didn't which seemed to do the exact same thing. I changed the top server block to this. Might be useful as I couldn't find an answer to this particular problem.

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

Upvotes: 0

Related Questions