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

JQuery Post随机出现502 Bad Gateway错误(NGINX/WinNMP环境)

随机出现502 Bad Gateway错误(WinNMP+PHP7.4+Nginx)

环境与问题现象

  • 运行环境:Windows系统,WinNMP套件运行Nginx,PHP版本7.4,前端使用jquery-3.6.0.min.js
  • 核心问题:POST请求tour-addons.php接口时随机触发502 Bad Gateway错误,频率约为每4次请求出现1次;Nginx无对应错误日志
  • Postman测试结果:同样报错,提示The server was acting as a gateway or proxy and received an invalid response from the upstream server

前端JQuery代码

$.post(IBE_URL+"/assets/ajax/tour-addons.php",
               payload,
               function(data, status){
                   $("#addon_canvas_body").html(data);
               });

当前Nginx配置

## For a live site, handling more connections, uncomment, then start WinNMP.exe --phpCgiServers=25:
#worker_processes auto;
#worker_rlimit_nofile 100000;

events {   
    ## For a live site, uncomment:
    #worker_connections 8096;
}

http {
        server_tokens       off;
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    ssi         off;
    server_names_hash_bucket_size  64;

    ## Timeouts ##
    ##############
    client_body_timeout 5;
    client_header_timeout   5;
    keepalive_timeout   25 25;
    send_timeout        15s;
    resolver_timeout    3s;

    # Timeout period for connection with FastCGI-server. It should be noted that this value can't exceed 75 seconds. 
    fastcgi_connect_timeout 5s;
    fastcgi_read_timeout    40s;
    fastcgi_send_timeout    15s;

    ## Buffers ##
    #############
    fastcgi_temp_file_write_size 10m;
    #fastcgi_busy_buffers_size 512k;
    fastcgi_buffer_size 512k;
    fastcgi_buffers 16 512k;

    open_file_cache         off;
    client_max_body_size        50m;    

    ####client_body_buffer_size 1K;
    client_header_buffer_size   8k;
    large_client_header_buffers 8 16k;
    types_hash_max_size     2048;

    include nginx.mimetypes.conf;
    default_type text/html;

    ## Logging ##
    #############
    access_log  "c:/winnmp/log/nginx_access.log";
    error_log   "c:/winnmp/log/nginx_error.log" warn;   #debug or warn
    log_not_found   on; #enables or disables messages in error_log about files not found on disk. 
    rewrite_log off;

    fastcgi_intercept_errors off;   # Do Not Change (off) !

    gzip  off;
    index  index.php index.htm index.html;

    server {
        listen      127.0.0.1:80    default_server;     # Do Not Change ! Security Risk !
        #listen     [::1]:80    ipv6only=on;        # Do Not Change ! Security Risk !
        server_name localhost;              # Do Not Change ! Security Risk !

        root        "c:/winnmp/www";
        autoindex on;
        
        allow       127.0.0.1;  # Do Not Change ! Security Risk !
        allow       ::1;        # Do Not Change ! Security Risk !
        deny        all;        # Do Not Change ! Security Risk !

        location ~ /\.ht {
            deny  all;
        }

        location = /favicon.ico {
                log_not_found off; 
        }
        location = /robots.txt {
                log_not_found off; 
        }

        ## Tools are now served from include/tools/
        location ~ ^/tools/.*\.php$ {                   
            root "c:/winnmp/include";
            try_files $uri =404; 
            include     nginx.fastcgi.conf;
            fastcgi_pass    php_farm;
            allow       127.0.0.1;      # Do Not Change ! Security Risk !
            allow       ::1;            # Do Not Change ! Security Risk !
            deny        all;            # Do Not Change ! Security Risk !
        }
        location ~ ^/tools/ {
            root "c:/winnmp/include";
            allow       127.0.0.1;      # Do Not Change ! Security Risk !
            allow       ::1;            # Do Not Change ! Security Risk !
            deny        all;            # Do Not Change ! Security Risk !
        }

        ## How to add phpMyAdmin 
        ## Copy phpMyAdmin files to c:/winnmp/include/phpMyAdmin then uncomment:
        #location ~ ^/phpMyAdmin/.*\.php$ {
        #   root "c:/winnmp/include";
        #   try_files $uri =404; 
        #   include         nginx.fastcgi.conf;
        #   fastcgi_pass    php_farm;
        #   allow           127.0.0.1;  
        #   allow           ::1;
        #   deny            all;
        #}       
        #location ~ ^/phpMyAdmin/ {
        #   root "c:/winnmp/include";
        #}

        ## PHP for localhost ##
        #######################
        location ~ \.php$ {
            try_files $uri =404; 
            include     nginx.fastcgi.conf;
            include     nginx.redis.conf;
            fastcgi_pass    php_farm;
            allow       127.0.0.1;      # Do Not Change ! Security Risk !
            allow       ::1;            # Do Not Change ! Security Risk !
            deny        all;            # Do Not Change ! Security Risk !
            }
    }

    include domains.d/*.conf;
    include nginx.phpfarm.conf;
}

已完成的排查动作

  • payload仅包含2个短字符串(最多5字符),无特殊字符
  • 将tour-addons.php简化为仅返回"ok"和200 HTTP状态码,问题仍存在
  • 调整fastcgi相关参数并重启Nginx,未解决问题
  • 拉长请求间隔至30秒,仍保持每4次请求失败一次的规律
  • 使用Postman测试,同样触发502错误

解决建议

  1. 增加PHP-CGI进程数:WinNMP默认启动的PHP进程数量较少,容易出现进程耗尽导致502。取消Nginx配置开头#worker_processes auto;和#worker_rlimit_nofile 100000;的注释,然后通过WinNMP.exe --phpCgiServers=25命令启动服务,提升PHP进程容量
  2. 检查PHP错误日志:查看WinNMP的PHP日志文件(一般路径为c:/winnmp/log/php_error.log),排查是否存在PHP进程崩溃、超时或其他异常记录
  3. 禁用Redis缓存测试:当前Nginx配置中引入了nginx.redis.conf,尝试注释该行#include nginx.redis.conf;,重启Nginx后测试,排除Redis相关问题
  4. 调整FastCGI连接超时:将fastcgi_connect_timeout从5秒调整为10秒,避免短时间内PHP进程未响应导致Nginx报错
  5. 检查进程与端口状态:打开任务管理器查看PHP-CGI进程是否存在崩溃情况,同时确认是否有其他程序占用了PHP-FPM的运行端口(可查看nginx.phpfarm.conf中的配置细节)

内容的提问来源于stack exchange,提问作者MadB

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 02:10:16