Redirect www and http to https in nginx

One canonical address for your site, everything else gets a 301.

SNP 037 #529 Filed nginx cmd

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

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.com;
    # ssl_certificate lines here
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com;
    # ssl_certificate lines here
    # the actual site
}

Test the config before reloading, nginx is unforgiving about typos:

shell
sudo nginx -t && sudo systemctl reload nginx