You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx & PHP-FPM每日两次502 Bad Gateway问题求助(服务器资源充足)

服务器每日出现两次502 Bad Gateway错误的排查与优化方案

服务器每日出现两次502 Bad Gateway错误,已尝试多项配置调整但无效,服务器资源状态正常。

服务器硬件配置

  • 2x E5-2670 0 @ 2.60GHz [32核]
  • 32 GB RAM

PHP-FPM日志

[07-Oct-2022 11:53:19] WARNING: [pool dsimlife-user] child 140615, script '/home/dsimlife/public_html/api.php' (request: "POST /api.php?f=enterServer") executing too slow (5.191753 sec), logging
[07-Oct-2022 11:53:19] WARNING: [pool dsimlife-user] child 140613, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runTask") executing too slow (5.108451 sec), logging
[07-Oct-2022 11:53:19] WARNING: [pool dsimlife-user] child 140609, script '/home/dsimlife/public_html/id1/api.php' (request: "POST /api.php?f=registerCitizen") executing too slow (5.151089 sec), logging
[07-Oct-2022 11:53:19] WARNING: [pool dsimlife-user] child 140606, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (5.204648 sec), logging
[07-Oct-2022 11:53:19] WARNING: [pool dsimlife-user] child 140604, script '/home/dsimlife/public_html/id1/api.php' (request: "POST /api.php?f=citizenWork") executing too slow (5.174349 sec), logging
[07-Oct-2022 11:54:21] WARNING: [pool dsimlife-user] child 140635, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (5.840929 sec), logging
[07-Oct-2022 11:54:21] WARNING: [pool dsimlife-user] child 140632, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (6.222622 sec), logging
[07-Oct-2022 11:54:21] WARNING: [pool dsimlife-user] child 140627, script '/home/dsimlife/public_html/id1/api.php' (request: "POST /api.php?f=citizenWork") executing too slow (6.403859 sec), logging
[07-Oct-2022 11:55:14] WARNING: [pool dsimlife-user] child 140613, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runTask") execution timed out (120.127614 sec), terminating
[07-Oct-2022 11:55:14] WARNING: [pool dsimlife-user] child 140606, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") execution timed out (120.223811 sec), terminating
[07-Oct-2022 11:55:14] WARNING: [pool dsimlife-user] child 140606 exited on signal 15 (SIGTERM) after 124.885829 seconds from start
[07-Oct-2022 11:55:14] WARNING: [pool dsimlife-user] child 140613 exited on signal 15 (SIGTERM) after 122.681948 seconds from start
[07-Oct-2022 11:55:21] WARNING: [pool dsimlife-user] child 140739, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (5.820752 sec), logging
[07-Oct-2022 11:55:21] WARNING: [pool dsimlife-user] child 140737, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (6.638456 sec), logging
[07-Oct-2022 11:55:21] WARNING: [pool dsimlife-user] child 140736, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runTask") executing too slow (6.573738 sec), logging
[07-Oct-2022 11:56:16] WARNING: [pool dsimlife-user] child 140632, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") execution timed out (121.254372 sec), terminating
[07-Oct-2022 11:56:16] WARNING: [pool dsimlife-user] child 140632 exited on signal 15 (SIGTERM) after 121.322812 seconds from start
[07-Oct-2022 11:56:21] WARNING: [pool dsimlife-user] child 140635, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (6.181178 sec), logging
[07-Oct-2022 11:56:23] WARNING: [pool dsimlife-user] child 140747, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runTask") executing too slow (6.635953 sec), logging
[07-Oct-2022 11:57:16] WARNING: [pool dsimlife-user] child 140739, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") execution timed out (120.846432 sec), terminating
[07-Oct-2022 11:57:16] WARNING: [pool dsimlife-user] child 140737, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") execution timed out (121.664136 sec), terminating
[07-Oct-2022 11:57:16] WARNING: [pool dsimlife-user] child 140737 exited on signal 15 (SIGTERM) after 121.690783 seconds from start
[07-Oct-2022 11:57:16] WARNING: [pool dsimlife-user] child 140739 exited on signal 15 (SIGTERM) after 120.927065 seconds from start
[07-Oct-2022 11:57:21] WARNING: [pool dsimlife-user] child 140736, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (5.359786 sec), logging
[07-Oct-2022 11:57:23] WARNING: [pool dsimlife-user] child 140758, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (6.440587 sec), logging
[07-Oct-2022 11:57:23] WARNING: [pool dsimlife-user] child 140757, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runTask") executing too slow (6.529727 sec), logging
[07-Oct-2022 11:58:16] WARNING: [pool dsimlife-user] child 140635, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") execution timed out (121.203518 sec), terminating
[07-Oct-2022 11:58:16] WARNING: [pool dsimlife-user] child 140635 exited on signal 15 (SIGTERM) after 241.053774 seconds from start
[07-Oct-2022 11:58:23] WARNING: [pool dsimlife-user] child 140766, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (6.561763 sec), logging
[07-Oct-2022 11:58:23] WARNING: [pool dsimlife-user] child 140747, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") executing too slow (6.515532 sec), logging
[07-Oct-2022 11:59:14] WARNING: [pool dsimlife-user] child 140831, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runTask") executing too slow (5.697952 sec), logging
[07-Oct-2022 11:59:16] WARNING: [pool dsimlife-user] child 140736, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") execution timed out (120.390085 sec), terminating
[07-Oct-2022 11:59:18] WARNING: [pool dsimlife-user] child 140758, script '/home/dsimlife/public_html/id1/api-bot.php' (request: "GET /api-bot.php?f=runWork") execution timed out (121.471340 sec), terminating

Nginx配置

user  nobody;
worker_processes 1;
worker_rlimit_nofile 2500;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid     /var/run/nginx.pid;
include /usr/local/apps/nginx/etc/modsecurity*.conf;

events {
    worker_connections  1024;
}

error_log /usr/local/apps/nginx/var/log/error_log debug;


http {
    include    mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /usr/local/apps/nginx/var/log/web.access.log  main;

    sendfile        on;
    #tcp_nopush  on;

    #keepalive_timeout  0;
    keepalive_requests 200;
    keepalive_timeout  130;

    #gzip  on;
    
    client_max_body_size 200M;
    
    # If your domain names are long, increase this parameter.
    server_names_hash_bucket_size 64;
    
    # To hide the version number in headers
    server_tokens off;
    
    include /usr/local/apps/nginx/etc/conf.d/*.conf;
}

PHP-FPM配置

pm.max_children = 5
pm.start_servers = 0
pm.min_spare_servers = 1
pm.max_spare_servers = 5
pm.max_requests = 20
pm.process_idle_timeout = 10
request_terminate_timeout = 120s

问题根源分析

从日志和配置来看,核心问题在于:

  1. PHP-FPM进程数严重不足:32核服务器仅配置pm.max_children=5,请求高峰期所有进程被慢请求占用,新请求排队等待,超时后PHP-FPM终止进程,Nginx返回502。
  2. 大量接口执行缓慢:/api-bot.php的runWork、runTask等请求频繁触发慢日志,最终超时,进一步加剧进程占用。
  3. Nginx未充分利用多核资源:worker_processes=1无法发挥32核服务器性能,同时缺少FastCGI超时配置,PHP-FPM超时后直接返回502。

配置优化方案

1. PHP-FPM配置调整

修改对应pool的配置文件(如www.conf):

pm.max_children = 32        # 匹配CPU核心数,充分利用多核资源
pm.start_servers = 8        # 初始启动进程数,建议为max_children的1/4
pm.min_spare_servers = 4    # 维持最小空闲进程数,避免频繁创建销毁进程
pm.max_spare_servers = 16   # 限制最大空闲进程数,防止资源浪费
pm.max_requests = 500       # 提高每个进程处理的请求数,减少进程重启频率
pm.process_idle_timeout = 30s  # 延长空闲进程超时时间,避免高峰时进程不足
request_terminate_timeout = 180s  # 适当延长超时时间,同时必须优化慢请求代码

2. Nginx配置调整

user  nobody;
worker_processes auto;       # 自动匹配CPU核心数,充分利用多核
worker_rlimit_nofile 10240; # 提高文件描述符限制,支持更多并发连接

pid     /var/run/nginx.pid;
include /usr/local/apps/nginx/etc/modsecurity*.conf;

events {
    worker_connections  2048; # 提高单进程连接数
    use epoll;                # 启用epoll事件驱动,适配高并发场景
}

error_log /usr/local/apps/nginx/var/log/error_log debug;

http {
    include    mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /usr/local/apps/nginx/var/log/web.access.log  main;

    sendfile        on;
    keepalive_requests 200;
    keepalive_timeout  130;

    client_max_body_size 200M;
    server_names_hash_bucket_size 64;
    server_tokens off;

    # 新增FastCGI超时配置,避免Nginx提前返回502
    fastcgi_connect_timeout 60s;
    fastcgi_send_timeout 180s;
    fastcgi_read_timeout 180s;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    
    include /usr/local/apps/nginx/etc/conf.d/*.conf;
}

3. 慢请求代码优化

针对日志中频繁超时的接口:

  • 排查/api-bot.php的runWork、runTask逻辑,优化数据库查询(添加索引、拆分复杂查询)
  • 减少外部API同步调用,改用异步处理(如消息队列)
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 15:55:14