Nginx 安全响应头与 TLS 配置清单
HSTS、CSP、X-Frame-Options 到底该怎么写?TLS 1.3 怎么开?为什么加了 CSP 页面白屏?给出一份可直接粘贴、逐行解释的配置。
安全响应头是「浏览器侧的最后一道防线」,成本极低、收益极高;CSP 要先 Report-Only 观察再强制,否则一定白屏。
1. 基础响应头
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
always 很重要,否则 4xx/5xx 响应不会带上这些头,而错误页恰恰是攻击者常利用的页面。
2. HSTS:先小 TTL,再拉长
add_header Strict-Transport-Security "max-age=15552000; includeSubDomains" always;
第一次上线建议 max-age=300,确认全站 HTTPS 无 Mixed Content 后再改成半年以上。不要一上来就加 preload——一旦提交预加载列表,回退非常麻烦,子域名没证书会直接打不开。
3. CSP:从 Report-Only 开始
add_header Content-Security-Policy-Report-Only "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self'; report-uri /csp-report;" always;
观察一到两周,把上报里出现的第三方域名补进白名单,再切换成强制模式。常见坑:
- 内联
onclick、style=""会被拦,需要 nonce 或 hash; - 百度统计、客服脚本、地图 SDK 都要显式放行;
unsafe-inline与 nonce 同时存在时,nonce 会让unsafe-inline失效。
4. TLS 配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 223.5.5.5 119.29.29.29 valid=300s;
resolver_timeout 5s;
用 openssl s_client -connect www.example.com:443 -servername www.example.com 检查证书链是否完整——缺中间证书在部分安卓机上会直接报错。
5. 关掉不该开的东西
server_tokens off;
autoindex off;
client_max_body_size 20m;
# 只允许必要方法
if ($request_method !~ ^(GET|HEAD|POST)$) { return 405; }
# 隐藏文件
location ~ /\.(?!well-known) { deny all; }
# 备份与配置
location ~* \.(sql|bak|conf|ini|log|swp)$ { deny all; }
6. 上传目录禁止执行
location ^~ /uploads/ {
location ~ \.(php|php5|phtml|jsp|asp|aspx|cgi|pl)$ { deny all; }
}
只禁后缀不够,还要保证 Nginx 不会把 shell.jpg/x.php 当成 PHP 执行——这取决于 fastcgi_split_path_info 与 try_files 的写法。最稳的是上传目录单独一个 location,直接不给 PHP handler。
7. 真实 IP 与日志
套了 CDN 后,$remote_addr 全是 CDN 节点。要么开 real_ip_module,要么在日志里同时记录 $http_x_forwarded_for。否则封禁封错人、溯源查不到源头。
set_real_ip_from 173.245.48.0/20;
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
8. 一份最小可用模板
server {
listen 443 ssl http2;
server_name www.example.com;
root /www/wwwroot/example;
index index.php index.html;
ssl_certificate /etc/letsencrypt/live/example/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
add_header Strict-Transport-Security "max-age=15552000" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
server_tokens off;
autoindex off;
location / { try_files $uri $uri/ /index.php?$query_string; }
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
curl -I https://www.example.com 逐项核对响应头;CSP 用浏览器控制台看拦截记录;TLS 用 openssl s_client 或在线扫描复核。本文由站点编辑整理,配置与命令已在测试环境验证;不同发行版与版本可能存在差异,执行前请先在非生产环境演练。