首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >502坏网关: nginx,php5-fpm,175/209 Connection ()失败(111:连接被拒绝),同时连接上游

502坏网关: nginx,php5-fpm,175/209 Connection ()失败(111:连接被拒绝),同时连接上游
EN

Stack Overflow用户
提问于 2016-03-03 15:17:55
回答 2查看 4.4K关注 0票数 5

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

拥有64 GB RAM和16核的旧服务器完全处于休眠状态,因为它上没有真正的流量。除非我们已经修复了像这样的所有错误,否则我们目前使用它就像一个分期系统。

错误日志:

在nginx错误日志中,可以找到以下行:

代码语言:javascript
复制
[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连接“错误,最后是一个"*209连接”

配置文件:

我将尝试只发布与此主题相关的重要行,并将删除所有被注释掉的行。

/etc/php5-FPM/info.gov.hk.d/www.conf:

代码语言:javascript
复制
[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:

代码语言:javascript
复制
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/站点-现有/站点。

代码语言:javascript
复制
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-设置后,nginx-log中的错误更改为:

代码语言:javascript
复制
30 upstream timed out (110: Connection timed out) while reading response header from upstream

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-03 16:35:42

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

另外,您的pm.max_children太低了,除非您希望将服务器限制在最多5个同时进行的php请求:http://php.net/manual/en/install.fpm.configuration.php

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

对于nginx.conf,请更改以下值:

代码语言:javascript
复制
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)

代码语言:javascript
复制
[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$ {块中添加以下内容:

代码语言:javascript
复制
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文件中的以下值更改为此并重新启动:

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2021-04-30 23:42:36

尝试绑定到0.0.0.0:9000:

代码语言:javascript
复制
listen = 0.0.0.0:9000
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35776015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档