Nginx配置踩坑与性能调优

📅 发布时间:2026/7/4 18:37:48 👁️ 浏览次数:
Nginx配置踩坑与性能调优
前言Nginx配置看起来简单但坑真的多。一个斜杠的差异、location匹配顺序、超时设置不当都可能导致莫名其妙的问题。这篇整理Nginx常见的配置错误和性能优化方案都是生产环境验证过的配置。一、配置踩坑1.1 proxy_pass结尾的斜杠这个坑很常见。# 前端请求 /api/users # 写法1 location /api { proxy_pass http://backend; } # 后端收到/api/users # 写法2多了个斜杠 location /api { proxy_pass http://backend/; } # 后端收到/users ← /api没了就差一个斜杠效果完全不一样。简单记proxy_pass后面带路径包括单独的/location匹配的部分会被替换掉不带路径原样转发。1.2 location匹配顺序location /api { proxy_pass http://backend; } location ~ /api/v[0-9] { proxy_pass http://backend-new; }访问/api/v2/users你觉得走哪个很多人以为按顺序其实不是。正则匹配~优先于普通前缀匹配。所以走的是第二个。但如果你想让某个路径不走正则location ^~ /api/internal { # ^~ 会阻止后面的正则匹配 proxy_pass http://internal-backend; } location ~ /api { proxy_pass http://backend; }^~前缀匹配的优先级比正则高。完整优先级精确 ^~前缀 ~正则 普通前缀取最长1.3 Vue/React项目刷新404单页应用部署后直接访问首页没问题但刷新或直接访问子路由就404。location / { root /var/www/dist; try_files $uri $uri/ /index.html; }这个配置应该没问题但还是404检查这几个1、index.html存在吗ls-la /var/www/dist/index.html2、SELinuxCentOS的大坑getenforce# 如果是Enforcing# 临时关闭测试setenforce0# 或者正确设置contextchcon -R -t httpd_sys_content_t /var/www/distSELinux是CentOS上的常见坑。1.4 502 Bad Gateway高并发的时候出现502后端其实没挂。通常是upstream的连接数不够了。加上keepaliveupstream backend { server 127.0.0.1:8080; keepalive 100; # 保持100个空闲连接 } location / { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Connection ; # 这行很重要 }Connection 是为了让Nginx用HTTP/1.1的keepalive不然每次请求都新建连接。1.5 上传大文件413413 Request Entity Too LargeNginx默认只允许1M的请求体client_max_body_size 100m;放在http、server或location块都行。1.6 WebSocket连接断开WebSocket连上没多久就断了通常是超时location /ws { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; # 关键超时设成长一点 proxy_read_timeout 3600s; proxy_send_timeout 3600s; }默认60秒没数据就断了WebSocket要设长一点。1.7 缓存不生效location ~* \.(js|css|png)$ { expires 30d; }配置了但浏览器还是每次都请求检查后端有没有返回Cache-Control: no-cache这会覆盖你的设置。location ~* \.(js|css|png)$ { proxy_pass http://backend; proxy_ignore_headers Cache-Control Expires; # 忽略后端的 expires 30d; }二、性能调优2.1 worker配置worker_processes auto; # 自动等于CPU核数 events { worker_connections 10240; use epoll; multi_accept on; }2.2 Gzip压缩gzip on; gzip_min_length 1k; # 太小的不压缩 gzip_comp_level 5; # 压缩级别5够用了 gzip_types text/plain text/css application/json application/javascript;2.3 静态文件缓存location ~* \.(js|css|png|jpg|gif|ico)$ { expires 1y; access_log off; # 静态文件不记日志省IO }2.4 sendfile零拷贝sendfile on; # 零拷贝 tcp_nopush on; # 合并小包 tcp_nodelay on;三、配置模板3.1 反向代理server { listen 80; server_name api.example.com; location / { 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; } }3.2 前端SPAserver { listen 80; server_name www.example.com; root /var/www/dist; location / { try_files $uri $uri/ /index.html; } location /api { proxy_pass http://127.0.0.1:8080; } location ~* \.(js|css|png|jpg|svg|woff2)$ { expires 1y; } }四、调试技巧4.1 检查配置nginx -t# 语法检查nginx -T# 打印完整配置4.2 打印变量不确定变量值是什么的时候location /debug { return 200 uri$uri\nhost$host\nargs$args\n; }4.3 查看连接状态location /status { stub_status on; allow 127.0.0.1; deny all; }然后curl localhost/status看当前连接数。总结Nginx配置常见问题问题原因解决502/504upstream连接问题检查后端、增加超时、keepalive413文件太大client_max_body_size404location匹配/路径错误检查root/alias、try_files缓存不生效后端覆盖proxy_ignore_headersWebSocket断开超时/头部问题设置upgrade头和超时性能优化核心系统层worker数、连接数、文件描述符传输层Gzip、sendfile缓存层静态文件expires、代理缓存连接层keepalive有问题评论区交流。