lolly/docs/config/ssl/hsts.conf
xfy 6543422281 docs: 添加 Nginx 配置和 Lua 脚本示例文档
- config: 反向代理、缓存、负载均衡、安全、SSL 等配置模板
- lua: API 网关、认证、动态路由、限流、WebSocket 等脚本示例

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 17:59:22 +08:00

76 lines
2.2 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ============================================================
# Nginx HSTS 安全配置示例
# ============================================================
#
# 功能说明:
# - HTTP Strict Transport Security 头部
# - 强制浏览器使用 HTTPS 连接
# - 防止协议降级攻击
#
# Lolly 对应配置:
# server:
# ssl:
# hsts:
# max_age: 31536000 # 1年
# include_sub_domains: true
# preload: false
# ============================================================
server {
listen 443 ssl http2;
server_name hsts.example.com;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
# HSTS 头部配置
# Lolly 对应: ssl.hsts 配置块
# max_age: 过期时间(秒),建议至少 6 个月15768000
# includeSubDomains: 包含所有子域名
# preload: 加入浏览器预加载列表(需提交到 hstspreload.org
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# 注意:
# 1. 首次设置建议使用较短 max-age如 300确认无问题后再延长
# 2. preload 需要满足条件并主动提交到 hstspreload.org
# 3. always 参数确保即使响应是错误页面也发送 HSTS 头
location / {
root /var/www/html;
}
}
# HTTP 重定向(配合 HSTS
server {
listen 80;
server_name hsts.example.com;
# 301 永久重定向
# HSTS 首次访问时仍然可能通过 HTTP需要重定向配合
return 301 https://$host$request_uri;
}
# HSTS 配置最佳实践:
#
# 1. 分阶段部署:
# 第一阶段: max-age=300 (5分钟)
# 第二阶段: max-age=86400 (1天)
# 第三阶段: max-age=604800 (1周)
# 最终阶段: max-age=31536000 (1年)
#
# 2. Preload 列表:
# 条件:
# - max-age >= 31536000 (1年)
# - includeSubDomains
# - preload
# - 所有域名都支持 HTTPS
# 提交: https://hstspreload.org
#
# 3. 移除 HSTS:
# 设置 max-age=0 可让浏览器清除 HSTS 设置
# 注意: 已加入 preload 列表的域名移除较困难
#
# 4. 测试:
# curl -I https://hsts.example.com
# # 检查 Strict-Transport-Security 头部