How to do it...

Like our previous redirects, using two server blocks is the most efficient method:

server { 
    listen 80; 
    server_name ssl.nginxcookbook.com; 
    return 301 https://ssl.nginxcookbook.com$request_uri; 
} 
 
server { 
    listen              443 ssl; 
    server_name         ssl.nginxcookbook.com; 
    ssl_certificate     /etc/ssl/public.pem; 
    ssl_certificate_key /etc/ssl/private.key; 
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2; 
    ssl_ciphers         HIGH:!aNULL:!MD5; 
 
    access_log  /var/log/nginx/ssl-access.log  combined; 
 
    location / { 
        root   /var/www; 
        index  index.html index.htm; 
    } 
} 

Here, we have our standard server block listening on port 80 and then redirecting all calls back to our calls to the HTTPS version. With the whole site redirected, even if there's a mistaken link to the HTTP version of the site, it will be automatically redirected.