Nginx 配置生成器:反向代理、PHP、WordPress、SPA、负载均衡、WebSocket
按场景生成可直接使用的 Nginx 配置,支持 HTTPS、HTTP/2、安全头,主要场景同时给出 Caddy 写法。
静态网站反向代理PHP-FPMWordPressNode.jsDjango / Python负载均衡WebSocketHTTPS 跳转单页应用 SPA压缩与缓存限流nginx.conf 主配置完整模式(nginxconfig.io 全部选项)→多站点打包 →CMS 伪静态规则(21 种)→
Nginx · 反向代理
建议保存为 /etc/nginx/conf.d/example.com.conf · 应用:nginx -t && systemctl reload nginx
# example.com — 由 nginx.cc 生成 · 反向代理
# 保存为 /etc/nginx/conf.d/example.com.conf,然后 nginx -t && systemctl reload nginx
# 以下 map 放在 http 层;多个站点重复写同样的 map 不会冲突
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# upstream keepalive 需要清空 Connection 头
map $http_upgrade $connection_upgrade_keepalive {
default upgrade;
'' '';
}
server {
listen 80;
listen [::]:80;
server_name example.com;
# Let's Encrypt 验证(证书续期也走这里)
location ^~ /.well-known/acme-challenge/ {
root /var/www/_letsencrypt;
}
location / {
return 301 https://example.com$request_uri;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# Mozilla Intermediate(Mozilla Server Side TLS 6.0)
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:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
# 不开 OCSP stapling:Let's Encrypt 已于 2025 年停止 OCSP 服务。
server_tokens off;
client_max_body_size 1m;
# 安全头
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 "camera=(), microphone=(), geolocation=()" always;
# CSP 需要按站点调整,确认不会挡掉自己的脚本后再启用:
# add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always;
add_header Strict-Transport-Security "max-age=63072000" always;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log warn;
# 隐藏文件(.git、.env 等)
location ~ /\.(?!well-known) {
deny all;
}
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 256;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml;
location / {
proxy_pass http://127.0.0.1:3010;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
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;
proxy_set_header X-Forwarded-Port $server_port;
}
}
阶段一配置(首次上线、证书还没签时用,只有 HTTP)
# example.com — 由 nginx.cc 生成 · 反向代理
# 保存为 /etc/nginx/conf.d/example.com.conf,然后 nginx -t && systemctl reload nginx
# 以下 map 放在 http 层;多个站点重复写同样的 map 不会冲突
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# upstream keepalive 需要清空 Connection 头
map $http_upgrade $connection_upgrade_keepalive {
default upgrade;
'' '';
}
server {
listen 80;
listen [::]:80;
server_name example.com;
server_tokens off;
client_max_body_size 1m;
# 安全头
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 "camera=(), microphone=(), geolocation=()" always;
# CSP 需要按站点调整,确认不会挡掉自己的脚本后再启用:
# add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log warn;
# Let's Encrypt 验证
location ^~ /.well-known/acme-challenge/ {
root /var/www/_letsencrypt;
}
# 隐藏文件(.git、.env 等)
location ~ /\.(?!well-known) {
deny all;
}
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 256;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml;
location / {
proxy_pass http://127.0.0.1:3010;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
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;
proxy_set_header X-Forwarded-Port $server_port;
}
}
首次上线步骤
- 安装或更新 Nginx(nginx.org 官方源)
curl -fsSL nginx.cc/install | sudo bash
- 阶段一:先上线 HTTP 版(证书还没签,HTTPS 配置会让 nginx -t 失败)
sudo mkdir -p /var/www/_letsencrypt # 把下方「阶段一配置」保存为 /etc/nginx/conf.d/example.com.conf sudo nginx -t && sudo systemctl reload nginx
- 签发 Let's Encrypt 证书(webroot 方式,不改动 nginx 配置)
sudo certbot certonly --webroot -w /var/www/_letsencrypt -d example.com --cert-name example.com \ --email you@example.com --agree-tos --no-eff-email \ --deploy-hook "systemctl reload nginx"
也可以用 certbot certonly --nginx -d example.com --cert-name example.com(同样不改配置);不建议 certbot --nginx 直接改写本配置。DNS 必须已指向本机,80 端口要放行。 - 阶段二:换成完整 HTTPS 配置
# 把「完整配置」覆盖保存到 /etc/nginx/conf.d/example.com.conf sudo nginx -t && sudo systemctl reload nginx
- 确认自动续期
sudo certbot renew --dry-run
- 上线后自检
curl -sI https://example.com | head -20 curl -fsSL nginx.cc/fix | sudo bash # 只读体检
也可以在 nginx.cc 在线诊断里检查安全头、跳转和压缩。
Caddy 写法
/etc/caddy/Caddyfile · caddy validate && systemctl reload caddy
example.com {
encode zstd gzip
header {
X-Content-Type-Options nosniff
X-Frame-Options SAMEORIGIN
Referrer-Policy strict-origin-when-cross-origin
Permissions-Policy "camera=(), microphone=(), geolocation=()"
Strict-Transport-Security "max-age=63072000"
-Server
}
# Caddy 自动处理 WebSocket 升级和 X-Forwarded-* 头
reverse_proxy 127.0.0.1:3010
}
说明
- 证书路径按 certbot 默认写法(/etc/letsencrypt/live/example.com/)。第一次部署请按「首次上线步骤」先用 HTTP 版签证书。
- 后端最好只监听 127.0.0.1。Express 需要 app.set('trust proxy', 1) 才能拿到真实 IP 和 https 协议。
常见问题
生成的配置可以直接用吗?
可以。每个场景的各种选项组合都在 Docker 里用 nginx -t 校验过,主要场景还用真实请求做了功能测试;部署时先运行 nginx -t 再 systemctl reload nginx。
旧版 nginx(Debian 12 / Ubuntu 24.04 自带)能用吗?
能。把「Nginx 版本」选成 < 1.25.1,会改用 listen 443 ssl http2 的旧写法,否则 http2 on 会报 unknown directive。
和 nginxconfig.io 有什么关系?
共享片段的结构参考了 nginxconfig.io(MIT,DigitalOcean),代码是重写的;完整模式覆盖它的全部选项并能直接导入它的分享链接,过时的写法换成了现代等价写法。