Debian 上用 Nginx 落地零信任的可执行方案
一、架构与原则
二、部署与配置步骤
步骤 1 前置加固(Nginx 基础安全)
server_tokens off;add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https:; object-src 'none'" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Strict-Transport-Security "max-age=63072000" always;
location ~ /\. { deny all; return 404; }
location ~* \.(env|git|bak|log|sql|zip|tar\.gz)$ { deny all; return 403; }
location ~ \.\./ { deny all; return 403; }
server {
listen 80 default_server;
listen 443 ssl default_server;
server_name _ "";
return 403;
}
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn addr 100;
limit_req_zone $binary_remote_addr zone=req:10m rate=10r/s burst=20;
limit_req zone=req burst=20 nodelay;
server {
listen 80;
server_name your.domain;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name your.domain;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
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_dhparam /etc/nginx/ssl/dhparam.pem;
}
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo nginx -t && sudo systemctl reload nginx。步骤 2 部署身份与策略决策点(Authelia)
https://auth.your.domain 与 https://your.domain/oauth2/callback),确保 HTTPS 与 DNS 正确解析。步骤 3 配置 Nginx 作为安全网关(与 Authelia 联动)
upstream backend {
server 127.0.0.1:8080; # 你的业务服务
}
server {
listen 443 ssl;
server_name your.domain;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
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;
# 1) 认证前置:将受保护路径先交给 Authelia
location / {
# 若使用 forward_auth(推荐,性能更好)
auth_request /authelia;
auth_request_set $user $upstream_http_remote_user;
auth_request_set $email $upstream_http_remote_email;
auth_request_set $groups $upstream_http_remote_groups;
proxy_pass http://backend;
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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header Remote-User $user;
proxy_set_header Remote-Email $email;
proxy_set_header Remote-Groups $groups;
}
# 2) Authelia 认证端点(内部 location,不对外暴露)
location = /authelia {
internal;
proxy_pass http://127.0.0.1:9091/api/authz/forward-auth;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Original-Method $request_method;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
# 3) 可选:登录与静态资源(Authelia 自身)
location /auth/ {
proxy_pass http://127.0.0.1:9091;
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;
proxy_set_header X-Forwarded-Host $host;
}
}
location / 的 auth_request 仅加在对应路径上。步骤 4 后端加固与网络最小暴露
sudo iptables -A INPUT -p tcp -s <NGINX_IP> --dport 8080 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8080 -j DROP
三、验证与运维
验证清单
curl -I https://your.domain/运维要点