Nginx HTTPS 重定向最佳实践

将 HTTP 流量重定向到 HTTPS 是网站安全的基本要求。本文聚焦 Nginx 中的最佳实践配置。 标准配置(推荐) 使用两个 server 块分离 HTTP 和 HTTPS 流量: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # HTTP server - 重定向到 HTTPS server { listen 80; server_name example.com www.example.com; return 301 https://$host$request_uri; } # HTTPS server server { listen 443 ssl http2; server_name example.com www.example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # HSTS - 告诉浏览器始终使用 HTTPS add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; root /var/www/html; index index....

March 26, 2026 · 2 min · 黑豆子