nginx + php5-fpm 中的错误 502

2022-08-30 11:08:49

我有一些错误与subj。服务器负载不高:~15%的CPU,有几Gb的内存,HDD不繁忙。但错误 502 在大约 3% 的情况下会引发。

程序: Debian 6, nginx/0.7.62, php5-fpm (5.3.3-1).

错误.log nginx是这个错误:

connect() to unix:/var/run/php5-fpm.sock failed

php5-fpm 的状态通常如下所示:

accepted conn:   41680
pool:             www
process manager:  dynamic
idle processes:   258
active processes: 1
total processes:  259

我认为,这意味着负载不高。

我增加了积压参数:在sysctl - net.core.somaxconn = 5000,在php-fpm pool中 - listen.backlog = 5000。无效果。

我引用一个配置:

/etc/nginx/nginx.conf

user www-data;
worker_processes  8;
timer_resolution 100ms;
worker_rlimit_nofile 20240;
worker_priority -5;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  2048;
    use epoll;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_min_length 1100;
    gzip_buffers 64 8k;
    gzip_comp_level 3;
    gzip_http_version 1.1;
    gzip_proxied any;
    gzip_types text/plain application/xml application/x-javascript text/css;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    client_max_body_size 100M;
    server_tokens off;
}

/etc/nginx/php_location

fastcgi_pass   unix:/var/run/php5-fpm.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
fastcgi_buffers 256 128k;
#fastcgi_buffer_size 16k;
#fastcgi_busy_buffers_size 256k;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
include fastcgi_params;

php-fpm pool

[www]
listen = /var/run/php5-fpm.sock
listen.backlog = 5000
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
user = www-data
group = www-data
pm = dynamic
pm.max_children = 1024
pm.start_servers = 64
pm.min_spare_servers = 64
pm.max_spare_servers = 128
pm.max_requests = 32000
pm.status_path = /system/php5-fpm-status
slowlog = /var/www/log/php-fpm.log.slow
chdir = /var/www

我能做些什么来优化这个系统,并让它使用所有的服务器资源?

对不起,我的英语不好。


答案 1

问题是插座本身,它在高负载工况下的问题是众所周知的。请考虑使用TCP\IP连接而不是unix套接字,为此您需要进行以下更改:

  • php-fpm 池配置中替换为listen = /var/run/php5-fpm.socklisten = 127.0.0.1:7777
  • /etc/nginx/php_location 中替换为fastcgi_pass unix:/var/run/php5-fpm.sock;fastcgi_pass 127.0.0.1:7777;

答案 2

在 Centos 7, Plesk 12.5

在我的硬盘已满并且某些服务失败后,我遇到了此问题。其他域工作完美,但没有一个它只给了我502和类似的超时。从错误日志中:

[crit] 3112#0: *65746768 connect() to 
unix:///var/www/vhosts/system/sub.domain.de/php-fpm.sock failed 
(2: No such file or directory) while connecting to upstream

为了解决这个问题,我不得不(首先腾出空间,然后)重新启动php-fpm和nginx - 然后这个错误消失了!


推荐