php-fpm.sock 连接到上游时失败(11:资源暂时不可用)

Has*_*aan 12 nginx php-fpm centos7

当我每秒测试 200 次点击时,我的网站上出现了以下错误。

首先我收到了 499 个错误

2017-04-09 03:22:45 错误 162.158.79.219 499 GET/HTTP/1.1 0 nginx 访问

2017-04-09 03:22:45 错误 162.158.79.87 499 GET/HTTP/1.1 0 nginx 访问

2017-04-09 03:22:45 错误 162.158.78.170 499 GET/HTTP/1.1 0 nginx 访问

2017-04-09 03:22:45 错误 162.158.78.68 499 GET/HTTP/1.1 0 nginx 访问

第二个错误开始显示 502

2017-04-09 03:22:45 错误 162.158.79.135 502 GET / HTTP/1.1 166 nginx 访问

2017-04-09 03:22:45 错误 162.158.79.225 502 GET / HTTP/1.1 166 nginx 访问

2017-04-09 03:22:45 错误 162.158.78.110 502 GET/HTTP/1.1 166 nginx 访问

2017-04-09 03:22:45 错误 162.158.79.225 502 GET / HTTP/1.1 166 nginx 访问

最后我开始收到php-fpm.sock failed错误

2017-04-09 03:22:45 错误 162.158.79.207 20699#0: *3826365 connect() to unix:///var/www/vhosts/system/playhdpk.top/php-fpm.sock 失败 (11:资源暂时不可用)同时连接到上游 nginx 错误

2017-04-09 03:22:45 错误 162.158.79.207 20695#0: *3826367 connect() to unix:///var/www/vhosts/system/playhdpk.top/php-fpm.sock 失败 (11:资源暂时不可用)同时连接到上游 nginx 错误

2017-04-09 03:22:45 错误 162.158.79.207 20697#0: *3826369 connect() to unix:///var/www/vhosts/system/playhdpk.top/php-fpm.sock 失败 (11:资源暂时不可用)同时连接到上游 nginx 错误

php-fpm-pool-settings在下面,我相信这个正在产生错误可能我错了

listen.backlog = 65535

;[php-fpm-pool-settings]
pm = dynamic
pm.max_children = 5000
pm.start_servers = 50
pm.min_spare_servers = 20
pm.max_spare_servers = 70
pm.max_requests = 2000
Run Code Online (Sandbox Code Playgroud)

我的nginx配置如下

user  nginx;
worker_processes 8;

# number of file descriptors used for nginx
# the limit for the maximum FDs on the server is usually set by the OS.
# if you don't set FD's then OS settings will be used which is by default 2000
worker_rlimit_nofile 99999;

error_log /var/log/nginx/error.log crit;

include /etc/nginx/modules.conf.d/*.conf;

events {

    worker_connections 16192;
    use epoll;
    multi_accept on;
}


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

    open_file_cache max=2048 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 5;
    open_file_cache_errors off;


    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_requests 100000;
    reset_timedout_connection on;
    client_body_timeout 30;
    send_timeout 15;

    client_header_timeout 12;
    proxy_connect_timeout  600s;
    proxy_send_timeout  600s;
    proxy_read_timeout  600s;

    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 256k;
    fastcgi_send_timeout 600s;
    fastcgi_read_timeout 600s;

    types_hash_max_size 2048;

    gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types application/x-javascript text/css application/javascript text/javascript text/plain text/xml application/json application/v$
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";


    server_tokens off;

    include /etc/nginx/conf.d/*.conf;
}
Run Code Online (Sandbox Code Playgroud)

注意:服务器规格如下

操作系统 CentOS 7.3

处理器:英特尔至强 E5-1620v2 - 4c/8t - 3.7 GHz/3.9 GH

服务器内存:64GB DDR3

Dan*_*nin 16

如果我可以永远拒绝@Artsiom 的答案,我会的。

pm.max_children = 4000意味着最多 4K 工作进程。如果流量非常快并且与 一起流动pm.max_requests = 0,工作人员永远不会被回收,RAM 使用量将随着时间的推移无限增长,服务器迟早会处于内存不足状态(停机、冻结)。

PHP-FPM max_children 应该在监视交换使用情况的同时小心地逐渐提高。

您可以使用以下公式:

pm.max_children = ((total RAM in MB) - (how much MySQL and others take in RAM)) / 80
Run Code Online (Sandbox Code Playgroud)

如果您的 PHP 框架很轻,那么 80 MB 是 PHP-FPM 工作进程的平均重量。对于像 Magento 2 这样的重物,至少需要 128 MB。

并且pm.max_requests应该是一些“有限”的值。在更高规格的服务器中,您确实可以提高它(例如 10000),而在低端服务器上,这应该设置为最小(例如 500,甚至 100)以减少 RAM“使用”波动。但在任何情况下我都不会将它设置为 0(无限制),因为值为 0 意味着您的代码/PHP 及其所有扩展完全没有内存泄漏。只有这样设置为0就好了!!!


小智 16

发生这种情况是因为操作系统拒绝 nginx 连接到 unix 套接字的尝试。

原因是超出了套接字连接的最大数量或未处理的套接字连接的最大数量。

检查限制:

sysctl net.core
Run Code Online (Sandbox Code Playgroud)

我们对以下几行感兴趣:

net.core.somaxconn = 128
net.core.netdev_max_backlog = 200
Run Code Online (Sandbox Code Playgroud)

由于它们,会发生错误,因为最大连接数为 128,最大未处理数为 200

更改限制,将这些行写入 /etc/sysctl.conf 文件中

nano /etc/sysctl.conf
Run Code Online (Sandbox Code Playgroud)

添加

net.core.somaxconn = 20000
net.core.netdev_max_backlog = 65535
Run Code Online (Sandbox Code Playgroud)

应用参数

sysctl -p
Run Code Online (Sandbox Code Playgroud)

重新启动 php-fpm

service php-fpm restart
Run Code Online (Sandbox Code Playgroud)

来源:https ://galaxydata.ru/community/sock-failed-11-resource-temporarily-unavailable-459


小智 -4

不限制请求 让他们免费工作)

pm = 点播
pm.max_children = 4000
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_requests = 0

  • @jgmjgm 不仅毫无意义,而且危险且完全不合理。这里的支持者应用了“Artsiom”的建议,但后来却陷入了悲伤。请参阅我的回答并进行解释。 (2认同)