Debian 上用 Nginx 与 SSL 落地零信任架构
一、架构与原则
二、部署步骤
sudo apt update && sudo apt install -y nginxsudo apt install -y certbot python3-certbot-nginx,执行 sudo certbot --nginx -d your.domain,自动配置与续期。/etc/letsencrypt/live/your.domain/fullchain.pem 与 privkey.pem。X-Frame-Options、X-Content-Type-Options、X-XSS-Protection);关闭不安全协议与弱套件。allow/deny 白名单;基于 HTTP Basic Auth 的账号口令;对管理路径叠加 二次验证(如 TOTP)。limit_req 按来源限速与突发控制,缓解暴力与爬虫;开启上游 keepalive 提升复用与稳定性。sudo nginx -t,通过后 sudo systemctl reload nginx;防火墙放行 80/443(如 sudo ufw allow 'Nginx Full')。三、Nginx 零信任配置示例
/admin 实施 IP 白名单 + Basic Auth + TOTP;其余路径仅 TLS 加固与限速。oathtool --totp -b 'YOUR_BASE32_SECRET'(保存密钥用于手机验证器)。sudo htpasswd -c /etc/nginx/.htpasswd alice。# 强制 HTTPS
server {
listen 80;
server_name your.domain;
return 301 https://$host$request_uri;
}
# 主站点:TLS 加固 + 限速
server {
listen 443 ssl http2;
server_name your.domain;
ssl_certificate /etc/letsencrypt/live/your.domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your.domain/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on; ssl_stapling_verify on;
resolver 8.8.8.8 valid=300s; resolver_timeout 5s;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
server_tokens off;
# 全局限速:每源 20 r/s,突发 40
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=20r/s;
limit_req_status 429;
location / {
limit_req zone=req_zone burst=40 nodelay;
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 敏感路径:IP 白名单 + Basic Auth + TOTP
location /admin {
# 示例:仅内网网段与管理机可直达
allow 10.0.0.0/8;
allow 192.168.1.0/24;
deny all;
# 基本认证
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/.htpasswd;
# 二次验证:TOTP(示例密钥,务必替换)
set $totp_secret "YOUR_BASE32_SECRET";
access_by_lua_block {
local totp = require "resty.totp"
local headers = ngx.req.get_headers()
local token = headers["X-TOTP"] or ""
local ok, err = totp.verify(totp_secret, token, 1) -- 1 个时间窗口容差
if not ok then
ngx.status = 403
ngx.say("Forbidden: invalid TOTP")
ngx.exit(ngx.HTTP_FORBIDDEN)
end
}
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
/admin 前置到支持 OpenID Connect/SAML 的身份代理(如 Authelia)实现统一二次验证。四、进阶与运维
auth_request 将未认证请求重定向至身份门户,实现“先鉴权、后放行”的零信任访问控制。