Nginx FAQ
内容:核心概念、反向代理、负载均衡、性能优化、故障排查、配置细节、HTTPS、HTTP/2、OpenResty 等。
- 基于 多进程 + 单线程 + 异步非阻塞事件驱动(epoll)
- master 负责管理,worker 处理请求
- 每个 worker 用异步 IO(epoll/kqueue)处理大量连接
- 无线程切换上下文开销
Nginx 采用了 “master + worker” 模型,worker 使用 epoll 事件驱动模型处理请求,异步非阻塞,无需为每个请求创建线程,因此上下文切换少、开销小,不会被慢连接阻塞,因此吞吐量高、延迟低。
- 反向代理:后端集群负载均衡
- 正向代理:代理客户端访问外网
- HTTP 服务器:用作 Web Server(静态资源)
- 负载均衡器:Round robin / IP hash / Least connections 等
- 缓存服务器:FastCGI cache / proxy cache
- API 网关 / ACL 限制 / WAF
标准算法:
- round robin(默认)
- weighted round robin
- least_conn(最少连接)
- ip_hash(粘性)
- hash(URI 级别 hash)
扩展:
- fair(第三方模块,按响应速度)
- consistent hash(一致性 Hash,upstream_chash)
- http 模块:反向代理、负载均衡
- stream 模块:TCP/UDP 四层代理
- mail 模块
- realip 模块:获取真实 IP
- ssl 模块
- gzip 模块
- rewrite 模块
- proxy / fastcgi / uwsgi 模块
| 指令 | 行为 |
|---|---|
| root | 会拼接 location 路径 |
| alias | 不会自动拼接路径 |
示例:
location /img/ {
root /data/web;
}
实际文件路径:/data/web/img/xxx.jpg
location /img/ {
alias /data/web/images/;
}
实际文件路径:/data/web/images/xxx.jpg
按顺序检查文件/目录是否存在,最后 fallback 到某个 URI。
常用于 SPA(如 React/ Vue):
try_files $uri $uri/ /index.html;
不带 /:
location /api/ {
proxy_pass http://backend;
}
会保留 /api/。
带 /:
proxy_pass http://backend/;
会截断 /api/。
新版 Nginx 是:
listen 443 ssl;
http2 on;
Nginx 解 TLS,后端使用 http:
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
需要 Upgrade 头:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
| 头部 | 含义 |
|---|---|
| X-Forwarded-For | 客户端 + 所有代理链路 IP |
| X-Real-IP | 最接近 Nginx 的上一跳 |
常用:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
当后端返回 302/Location 时路径不对:
Location: http://127.0.0.1/
改成:
Location: https://yourdomain.com/abc/
代表性例子:Jenkins、GitLab、Keycloak。
保持与后端的长连接,减少 TCP 建立成本:
upstream backend {
server 10.0.0.1;
keepalive 32;
}
提升高并发性能。
Nginx 开源版:无主动健康检查,只能通过失败尝试被动检查。
企业版 / openresty 的 lua + healthcheck 可以实现主动探测。
- ip_hash
- sticky cookie(第三方模块)
- upstream_hash(key)
通常与 CPU 核心数一致:
worker_processes auto;
最大并发 = worker_processes * worker_connections
- sendfile:零拷贝发送,提高静态文件性能
- tcp_nopush:配合 sendfile,让大包优先发送
- tcp_nodelay:用于实时响应(如小包)
推荐生产配置:
sendfile on;
tcp_nopush on;
tcp_nodelay on;
| 功能 | on | off |
|---|---|---|
| 缓冲后端响应 | 是 | 否 |
| 能加速用户? | 是 | 否 |
| 能保护后端? | 是 | 否 |
| WebSocket / SSE | 否 | 必须 off |
Jenkins 官方:proxy_buffering off;
- 不要压缩图片/video
- 只压缩文本类:html/json/css/js
- 避免压缩已压缩内容
502(Bad Gateway)
- 后端服务挂了
- 后端端口无法访问
- FastCGI 超时
- PHP-FPM 慢 / 崩溃
504(Gateway Timeout)
- 后端响应慢
- proxy_read_timeout 太小
常见原因:
- upstream 缓存
- CDN 缓存
- 浏览器缓存(强缓存)
- proxy_cache 未关闭
- 冗余 server block 优先级错误(端口/host)
核心调试日志:
error.log debug;
access.log combined;
debug 模式:
error_log /var/log/nginx/error.log debug;
基于 Nginx + LuaJIT 的高性能平台,可用于:
- API 网关
- 动态 WAF
- 复杂路由
- 动态限流
- 缓存系统
- 动态分流 / AB test
- 复杂鉴权
- token 校验
- 自定义负载均衡策略
- 动态 upstream
- 异步非阻塞事件驱动(epoll)
- worker 单线程无锁竞争
- 内存管理优秀(pool)
- 零拷贝 sendfile
- 多核利用率高(worker_processes auto)
- 稳定成熟
- master fork 出 workers
- worker 接收连接
- event 模型(epoll)将事件加入队列
- http 模块解析请求
- 匹配 location
- 转发到 upstream 或返回静态文件
- 记录日志
每一步都可被模块 hook。