关键是将证书配置、TLS参数和重定向逻辑下沉至Nginx全局层:用*.example.com通配符证书统一覆盖多语言子域,通过map动态映射域名到证书路径,http块中统设ssl_protocols、HSTS及301跳转规则,实现零配置新增子域自动HTTPS。
在多语言站点中统一管理 HTTPS 安全证书策略,关键是把证书配置、TLS 参数和重定向逻辑从具体站点剥离,下沉到 Nginx 全局层,避免为 zh.example.com、en.example.com、ja.example.com 等每个子域重复写一套 SSL 配置。
最简方案是使用一张泛域名证书(如 *.example.com),它天然支持所有子域,包括多语言子域:
/etc/nginx/ssl/example.com/fullchain.pem 和 /etc/nginx/ssl/example.com/privkey.pem
http 块中定义一次,后续所有 server 块直接复用,无需重复声明 ssl_certificate
若不同语言站点分属不同品牌或需独立证书(如 en.brand-a.com 和 zh.brand-b.com),可用 map 实现按域名自动加载证书:
http 块顶部定义映射关系:map $host $ssl_cert_path {
en.brand-a.com "/etc/nginx/ssl/brand-a/fullchain.pem";
zh.brand-b.com "/etc/nginx/ssl/brand-b/fullchain.pem";
default "/etc/nginx/ssl/default/fullchain.pem";
}
server 块中直接引用:ssl_certificate $ssl_cert_path;
ssl_certificate_key $ssl_cert_path/.key;
ssl_certificate_by_lua_block)所有语言站点应共用同一套加密强度与响应头,避免个别子域降级:
http 块中统一设置:ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:…;
ssl_prefer_server_ciphers off;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
新上线的语言子域无需修改任何 server 块,就能自动跳转:
http 块中用 map 定义跳转逻辑:map $scheme $redirect_to_https {
http "https://$host$request_uri";
default "";
}
server { listen 80; return 301 $redirect_to_https; }
fr.example.com)只要走这个 80 端口,就自动 301 跳转,无需单独配置