Question

NGINX - Redirect to HTTPS

I have been following the NGINX SSL with Let’s Encrypt tutorial and appear to have successfully configured SSL for my server, but it doesn’t appear to default to that. When I access my website with www.mysite.com it defaults to http, but if I use https://www.mysite.com, I am able to access the server, but it displays the NGINX server landing page (“Welcome to nginx!”) page on an initially created server. I followed the instructions outside of the ufw firewall setup, but I was wondering if that could actually be related to this problem or if a scenario like this would be related the NGINX server configuration. Can anyone provide some guidance?

Show comments

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.

Accepted Answer

@connordphillips

Here’s a new config for http to https redirect:

#This server block will redirect http:// to https://www.mysite.com
server {
    listen 80;
    listen [::]:80;
    server_name mysite.com www.mysite.com;
    return 301 https://www.mysite.com$request_uri;
}

#This server block will redirect https://mysite.com to https://www.mysite.com (expecting that you have a certificate for mysite.com as well as www.mysite.com
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-www.mysite.com.conf;
    include snippets/ssl-params.conf;
    server_name mysite.com;
    return 301 https://www.$server_name$request_uri;
}

#This is the server block actually delivering content to the visitor
server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    include snippets/ssl-www.mysite.com.conf;
    include snippets/ssl-params.conf;

    server_name www.mysite.com;

    client_max_body_size 100M;

    location ~ ^/\.well-known {
        root /var/www/ghost;
        allow all;
    }

    location / {
        proxy_pass http://127.0.0.1:2368;
        proxy_buffering off;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Referer "";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
    }
}

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.

Hi, I’m having the same problem but I’m not getting anywhere. The https site still shows the nginx webpage. :(

Here’s my nginx config (with stuff that was added by Certbot):

server {
        listen 85;
        listen [::]:85;
        server_name mydomain.com www.mydomain.com;
        return 301 https://mydomain.com$request_uri;
}
server {
        # SSL configuration

        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name mydomain.com;
        return 301 https://www.$server_name$request_uri;
}
server {

        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;

        root /var/www/html;

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

        server_name mydomain.com www.mydomain.com;

        location / {
                try_files $uri $uri/ =404;
        }
ssl_certificate /etc/letsencrypt/live/.../fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/.../privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}