server
{
    listen 80;
    server_name example.com www.example.com;
    return 301 https://www.example.com$request_uri;
}
server
{
    listen 443 ssl http2;
    server_name example.com;  
    ssl_certificate /www/ssl/example.com/fullchain.cer;
    ssl_certificate_key /www/ssl/example.com/example.com.key;
    return 301 https://www.example.com$request_uri;
}
server
{
    listen 443 ssl http2;
    server_name www.example.com;
    # OCSP stapling配置
    ssl_stapling on;
    # Let's encrypt 无需配置
    ssl_stapling_verify on;
    ssl_certificate /www/ssl/example.com/fullchain.cer;
    ssl_certificate_key /www/ssl/example.com/example.com.key;
    # 国内部分免费证书需要配置
    ssl_trusted_certificate  /path/to/cert.pem;

    index index.php index.html index.htm default.php default.htm default.html;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    root /www/wwwroot/example.com;
}

第一个server是http://example.com/和http://www.example.com/ 301跳转到https://www.example.com/;
第二个server是https://example.com/ 301重定向到https://www.example.com/;
第三个server是指定https://www.example.com/ 是ssl连接。

相关阅读:
https://blog.csdn.net/w670328683/article/details/79371504
https://www.wmsoho.com/lnmp-nginx-http-to-https-without-www/
https://www.cnblogs.com/sanwenyu/p/4579529.html
https://blog.csdn.net/ivy_99/article/details/83381942