Nginx 常见问题全解析:从 502 到 WebSocket 失败,如何逐一排查?

Nginx 作为一款高性能的 Web 服务器和反向代理,广泛应用于各种网站和应用架构中。然而,许多开发者和运维人员在使用过程中都会遇到 502 Bad Gateway413 Request Entity Too LargeNginx 负载均衡异常反向代理 WebSocket 失败 等一系列问题。

本篇文章将从 问题排查、错误分析、优化方案 等多个维度,带你深入解析 Nginx 常见问题,帮助你优化 Nginx 配置,提升网站稳定性。

1. 502 Bad Gateway?检查后端状态!

问题描述:

Nginx 作为反向代理服务器时,返回 502 Bad Gateway,通常意味着 Nginx 无法正确连接后端服务器

可能原因:

后端进程崩溃或未启动

端口号错误,Nginx 配置的 proxy_pass 地址无效

后端响应超时,导致 Nginx 断开连接

解决方案:

检查后端是否正常运行

调整 Nginx 超时参数,防止超时过短

location / {

    proxy_connect_timeout 60s;

    proxy_send_timeout 60s;

    proxy_read_timeout 60s;

}

2. 413 Request Entity Too Large?调整上传大小!

问题描述:

当用户上传大文件时,Nginx 返回 413 Request Entity Too Large,说明 请求体大小超出了限制

解决方案:

在 nginx.conf 里增加或修改 client_max_body_size 参数:

http {
    client_max_body_size 200M;
}

3. 504 Gateway Timeout?优化超时配置!

问题描述:

当 Nginx 代理后端服务时,若请求执行时间过长,可能会出现 504 Gateway Timeout

可能原因:

后端处理速度慢,超出 Nginx 设定的超时时间

Nginx proxy_read_timeout 或 fastcgi_read_timeout 设定过短

网络问题导致请求超时

解决方案:

在 Nginx 配置中适当延长超时时间:

http {

    proxy_connect_timeout 120s;

    proxy_send_timeout 120s;

    proxy_read_timeout 120s;

    fastcgi_send_timeout 120s;

    fastcgi_read_timeout 120s;

}

4. 负载均衡异常?调整 upstream 策略!

问题描述:

使用 Nginx 进行负载均衡时,可能会出现部分请求失败、负载分布不均等问题。

可能原因:

负载均衡策略不合理(如默认 round-robin 无法适应业务)

后端某些服务器负载过高或宕机,Nginx 仍然转发请求过去

解决方案:

优化 upstream 负载均衡配置

upstream backend {
    least_conn; # 选择最少连接的服务器
    server 192.168.1.1:8080 max_fails=3 fail_timeout=30s;
    server 192.168.1.2:8080 backup; # 备用服务器
}

增加健康检查(Nginx 默认无健康检查功能,建议使用 ngx_http_healthcheck_module)

5. Nginx 限流导致 503?调整 limit_conn!

问题描述:

如果 Nginx 配置了连接数或并发限制,可能会导致部分用户访问返回 503 Service Unavailable

可能原因:

limit_conn 过于严格,导致部分请求被拒绝

worker_connections 过低,无法处理大量并发

解决方案:

放宽 limit_conn 限制

http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;
}
server {
    limit_conn addr 100;
}

优化 worker_connections

events {

    worker_connections 8192;

}

提升 ulimit

ulimit -n 65535

6. WebSocket 代理失败?检查 Upgrade 头!

问题描述:

Nginx 作为反向代理时,WebSocket 连接可能失败,提示 Error during WebSocket handshake。

解决方案:

确保 Upgrade 头正确传递

location /ws/ {

    proxy_pass http://backend;

    proxy_http_version 1.1;

    proxy_set_header Upgrade $http_upgrade;

    proxy_set_header Connection "Upgrade";

}

7. Nginx 日志过大?启用日志轮转!

问题描述:

Nginx 日志长期积累,可能会占用大量磁盘空间,影响服务器性能。

解决方案:

使用 logrotate 进行日志切割:

vim /etc/logrotate.d/nginx

添加以下内容:

/var/log/nginx/*.log {
    daily
    rotate 14
    missingok
    compress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        systemctl reload nginx > /dev/null 2>&1
    endscript
}

总结

502 Bad Gateway → 检查后端服务是否正常,并调整超时时间

413 Request Entity Too Large → 调整 client_max_body_size 允许大文件上传

504 Gateway Timeout → 增加 proxy_read_timeout 避免超时

负载均衡异常 → 使用 least_conn 等优化 upstream 策略

连接数受限 → 调整 worker_connections 并优化 ulimit 限制

WebSocket 代理失败 → 确保 Upgrade 头正确传递

Nginx 日志过大 → 启用 logrotate 进行日志轮转

以上就是 Nginx 常见问题及优化方案,合理调整 Nginx 配置,可以显著提高网站的稳定性和访问速度!如果你遇到其他 Nginx 问题,欢迎留言讨论!

你可能感兴趣的:(nginx,nginx,websocket,运维)