nginx + php5-fpm中的错误502

时间:2012-05-06 11:18:08

标签: nginx fastcgi php

我对subj有一些错误。服务器没有高负载:~15%的CPU,有几个Gb的内存,硬盘不是buisy。但是错误502大约在3%的情况下抛出。

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

在nginx的error.log中出现此错误:

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池中 - 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

如何优化此系统并使其使用所有服务器资源?

PS。对不起,我的英语不好。

4 个答案:

答案 0 :(得分:104)

问题是socket本身,它在高负载情况下的问题是众所周知的。请考虑使用TCP \ IP连接而不是unix socket,因为您需要进行这些更改:

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

答案 1 :(得分:2)

On 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)然后这个错误消失了!

答案 2 :(得分:0)

未创建此文件的唯一原因是/etc/php-fpm.d/www.conf中的配置

更改listen = 127.0.0.1:9000

使用listen = /var/run/php-fpm/php-fpm.sock

然后重新启动nginx和php-fpm

答案 3 :(得分:-6)

我同样的问题,但不希望从套接字切换到TCP / IP。重启php-fpm和nginx将解决问题。

sudo /etc/init.d/php-fpm restart
sudo /etc/init.d/nginx restart