【问题标题】:502 Bad Gateway: nginx, php5-fpm, 175/209 connect() failed (111: Connection refused) while connecting to upstream502 Bad Gateway: nginx, php5-fpm, 175/209 connect() failed (111: Connection refused) 同时连接到上游
【发布时间】:2021-05-01 07:42:36
【问题描述】:

在带有 nginxphp5-fpm 的 Debian Jessie 机器上运行 shopware 5,我们经常得到 502 Bad Gateway。这主要发生在后端,当较长的操作像缩略图创建一样工作时,即使这是在单个 ajax 请求的小块中完成的。

具有 64 GB RAM 和 16 核的二手服务器完全处于休眠状态,因为它上面没有真正的流量。我们目前将其用作暂存系统,除非我们已经修复了像这样的所有错误。

错误日志:

在 nginx-error 日志中可以找到以下行:

[error] 20524#0: *175 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: domain.com, request: "POST /backend/MediaManager/createThumbnails HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com", referrer: "http://www.domain.com/backend/"

[error] 20524#0: *175 no live upstreams while connecting to upstream, client: xx.xx.xx.xx, server: domain.com, request: "POST /backend/Log/createLog HTTP/1.1", upstream: "fastcgi://php-fpm", host: "www.domain.com", referrer: "http://www.domain.com/backend/"

[error] 20524#0: *175 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: domain.com, request: "GET /backend/login/getLoginStatus?_dc=1457014588680 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com", referrer: "http://www.domain.com/backend/"

[error] 20522#0: *209 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: domain.com, request: "GET /backend/login/getLoginStatus?_dc=1457014618682 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com", referrer: "http://www.domain.com/backend/"

也许值得注意的是,一开始会出现很多 "*175 connect" 错误,然后最后出现 "*209 connect"

配置文件:

我将尝试仅发布与该主题相关的重要行,并忽略所有被注释掉的行。

php-fpm:

/etc/php5-fpm/pool.d/www.conf:

[www]
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

nginx:

/etc/nginx/nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    multi_accept on;
}

http {
    ## MIME types.
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ## Default log and error files.
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ## Use sendfile() syscall to speed up I/O operations and speed up
    ## static file serving.
    sendfile        on;

    ## Handling of IPs in proxied and load balancing situations.
    # set_real_ip_from        192.168.1.0/24; # set to your proxies ip or range
    # real_ip_header          X-Forwarded-For;

    ## Timeouts.
    client_body_timeout             60;
    client_header_timeout           60;
    keepalive_timeout            10 10;
    send_timeout                    60;

    ## Reset lingering timed out connections. Deflect DDoS.
    reset_timedout_connection on;

    ## Body size.
    client_max_body_size 10m;

    ## TCP options.
    tcp_nodelay        on;
    ## Optimization of socket handling when using sendfile.
    tcp_nopush         on;

    ## Compression.
    gzip              on;
    gzip_buffers      16 8k;
    gzip_comp_level   1;
    gzip_http_version 1.1;
    gzip_min_length   10;
    gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon application/vnd.ms-fontobject font/opentype application/x-font-ttf;
    gzip_vary         on;
    gzip_proxied      any; # Compression for all requests.
    gzip_disable      "msie6";

    ## Hide the Nginx version number.
    server_tokens off;

    ## Upstream to abstract backend connection(s) for PHP.
    upstream php-fpm {
        server unix:/var/run/php5-fpm.sock;
        # server 127.0.0.1:9000;

        ## Create a backend connection cache.
        keepalive 32;
    }

    ## Include additional configs
    include /etc/nginx/conf.d/*.conf;

    ## Include all vhosts.
    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-available/site.conf:

server {
    listen 80;
    listen 443 ssl;

    server_name xxxxxxxx.com;
    root /var/www/shopware;

    ## Access and error logs.
    access_log /var/log/nginx/xxxxxxxx.com.access.log;
    error_log  /var/log/nginx/xxxxxxxx.com.error.log;

    ## leaving out lots of shopware/mediafiles-related settings
    ## ....
    ## continue:

    location ~ \.php$ {
        try_files $uri $uri/ =404;

        ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        ## required for upstream keepalive
        # disabled due to failed connections
        #fastcgi_keep_conn on;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SHOPWARE_ENV    $shopware_env if_not_empty;
        fastcgi_param ENV             $shopware_env if_not_empty; # BC for older SW versions

        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;

        client_max_body_size 24M;
        client_body_buffer_size 128k;

        ## upstream "php-fpm" must be configured in http context
        fastcgi_pass php-fpm;
    }

}

现在该怎么办?如果我应该为这个问题提供更多信息,请现在告诉我。

更新

从@peixotorms 应用 nginx- 和 fpm-settings 后,nginx-logs 中的错误变为:

30 upstream timed out (110: Connection timed out) while reading response header from upstream

但问题本身并没有解决。它只是另一张脸......

【问题讨论】:

  • 我们在测试机器上遇到了同样的问题。最后我们发现它是由 php 扩展引起的,在我们的例子中是 opcache,删除该扩展解决了这个问题。因此,您可以尝试逐个禁用扩展程序并访问您的服务器以重现错误。
  • 在不完全检查当前配置的情况下,您可以检查github.com/bcremer/shopware-with-nginx 并检查(在测试环境中!!)是否设置。否则禁用 php 扩展对我来说非常有用。由于这个问题只是偶尔发生,这对我来说听起来很有趣。
  • @semm0:我们的配置与您提到的来源完全相同:github.com/bcremer/shopware-with-nginx :)
  • sry :) 任何可能限制上游连接的 psad / ufw 规则?例如Y 时间内的最大 X 个连接数?

标签: php nginx fastcgi shopware


【解决方案1】:

这对您来说可能听起来很奇怪,但您的问题很可能是由于您在套接字而不是 tcp 端口上运行 PHP。当您在套接字配置上对 php 有大约 300 个并发请求(有时更少)时,您将开始看到 502 错误(和其他错误)。

你的pm.max_children 也太低了,除非你想将你的服务器限制为最多 5 个同时 php 请求:http://php.net/manual/en/install.fpm.configuration.php

以这种方式配置它,这些错误应该会消失:

为您的 nginx.conf 更改以下值:

worker_processes 4; 
worker_rlimit_nofile 750000;

# handles connection stuff
events { 
worker_connections 50000;
multi_accept on;
use epoll;
}

upstream php-fpm {
        keepalive 30;
        server 127.0.0.1:9001;
}

你的/etc/php5-fpm/pool.d/www.conf

(使用这些设置是因为你有足够的内存和 CPU)

[www]
user = www-data
group = www-data
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
listen = 127.0.0.1:9001
listen.allowed_clients = 127.0.0.1
listen.backlog = 65000
pm = dynamic
pm.max_children = 1024
pm.start_servers = 8
pm.min_spare_servers = 4
pm.max_spare_servers = 16
pm.max_requests = 10000

在你的 location ~ \.php$ { 块中添加这个:

location ~ \.php$ {
        try_files $uri $uri/ =404;

        ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        ## required for upstream keepalive
        # disabled due to failed connections
        #fastcgi_keep_conn on;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SHOPWARE_ENV    $shopware_env if_not_empty;
        fastcgi_param ENV             $shopware_env if_not_empty; # BC for older SW versions

        fastcgi_keep_conn on;
        fastcgi_connect_timeout 20s; 
        fastcgi_send_timeout 60s; 
        fastcgi_read_timeout 60s;
        fastcgi_pass php-fpm;
    }

编辑:

/etc/php5/fpm/php.ini 文件中的以下值更改为此并重新启动:

safe_mode = Off    
output_buffering = Off
zlib.output_compression = Off
max_execution_time = 900
max_input_time = 900
memory_limit = 2048M
post_max_size = 120M
file_uploads = On
upload_max_filesize = 120M

【讨论】:

  • 虽然您的配置设置对我来说听起来很合理,但不幸的是它们并没有改变任何东西。是的,我重新启动了 fpm 和 nginx。我还重新启动了整个服务器。但我想,它一定与限制有关。当只有少数请求发送到服务器时,一切正常。如果它是一堆 ajax 请求,每个请求持续数秒,则错误的网关再次发生
  • 现在 nginx-log 中的错误更改为:30 upstream timed out (110: Connection timed out) while reading response header from upstrea
  • 这看起来像是 php 方面的问题,但只是为了确保将 fastcgi_read_timeoutfastcgi_send_timeout 更改为 60s 并删除 location ~ \.php$ { 上的 fastcgi_buffers, fastcgi_buffer_size, client_max_body_size, client_body_buffer_size (请参阅我的答案)。同时使用这些值编辑您的 php.ini 文件并重新启动。
  • 当它开始失败时,我们讨论了多少个 php 请求,每个请求应该运行多长时间?您的 mysql 脚本是否存在慢查询日志错误(假设您正在使用它们)。
  • 是的,你说的太对了!终于我明白了。这都是关于超时的。进一步的调查向我指出了一个更长的 ajax 请求,有时需要更长的时间然后配置超时。非常感谢!
【解决方案2】:

尝试绑定到 0.0.0.0:9000:

listen = 0.0.0.0:9000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2020-10-26
    • 2017-07-30
    • 1970-01-01
    • 2016-01-21
    • 2019-12-17
    相关资源
    最近更新 更多