Nginx 配置指南
这页适合作为“反向代理与静态站点配置入口”。Nginx 真正常用的不是花哨指令,而是把站点入口、代理、TLS、缓存和限流几件事稳定拼起来。
推荐上手顺序
建议按这个顺序掌握:
- 先会起一个静态站点
- 再会配反向代理
- 再接 HTTPS 和自动续期
- 再加缓存、压缩和限流
- 最后再做负载均衡和更细的性能调优
先把基础站点跑顺,比一开始追求复杂高性能配置更重要。
安装
# Ubuntu/Debian
apt install nginx -y
systemctl enable nginx
# Docker
docker run -d --name nginx -p 80:80 -p 443:443 nginx:alpine
常用命令
nginx -t # 测试配置
nginx -s reload # 重载配置
nginx -s stop # 停止
systemctl status nginx # 查看状态
基础配置
# /etc/nginx/sites-available/example.conf
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
启用站点:
ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/
nginx -t && nginx -s reload
常见场景
单页前端应用
前端路由项目通常需要 try_files 回退到入口文件,否则刷新子路由会 404。
反代后端 API
核心不是 proxy_pass 一条指令,而是把 Host、X-Forwarded-* 等头补齐,不然后端经常拿不到真实来源信息。
反向代理
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
WebSocket 支持
location /ws {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
HTTPS(Let's Encrypt)
配合 Certbot:
apt install certbot python3-certbot-nginx -y
certbot --nginx -d example.com -d www.example.com
手动配置 SSL:
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# 安全配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# HSTS
add_header Strict-Transport-Security "max-age=31536000" always;
}
# HTTP 重定向到 HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
负载均衡
upstream backend {
server 127.0.0.1:3001;
server 127.0.0.1:3002;
server 127.0.0.1:3003;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
静态文件与缓存
# 静态资源缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# Gzip 压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;
gzip_min_length 1000;
限流
# 定义限流区域
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
server {
location /api/ {
limit_req zone=api burst=20 nodelay;
proxy_pass http://127.0.0.1:3000;
}
}
上线前检查
部署前建议至少检查:
nginx -t通过- 80 / 443 端口已放行
- 证书路径和域名对应正确
- 后端服务本身在本机可访问
- 日志目录、静态目录和站点目录权限正常
常用变量
| 变量 | 说明 |
|---|---|
$host | 请求的主机名 |
$remote_addr | 客户端 IP |
$request_uri | 完整请求 URI |
$uri | 不含参数的 URI |
$args | 查询参数 |
$scheme | http 或 https |
常见问题
改完配置 reload 失败
优先跑:
nginx -t
大多数问题都能在语法检查里先发现。
反向代理后 WebSocket 不通
通常是:
- 没加
Upgrade - 没加
Connection "upgrade" - 代理层超时配置不合适
前端刷新 404
这类问题多见于 SPA,通常说明少了 try_files $uri $uri/ /index.html;。
HTTPS 好了,但站点资源还是混合内容
说明页面里还有 http:// 资源链接,或者代理层没有把 X-Forwarded-Proto 传给上游应用。
风险提醒
- 不要直接照搬网上整份大配置,先理解每一段在做什么
- 证书、私钥、日志和站点目录权限要分开检查
- 限流和缓存上线前先验证,避免误伤正常流量
延伸阅读
参考链接
- Nginx 文档 — 官方文档
- Nginx Config Generator — 在线生成配置
- Mozilla SSL Config — SSL 配置生成器