php nginx + php5-fpm 中的错误 502

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10470109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:15:45  来源:igfitidea点击:

Error 502 in nginx + php5-fpm

nginxfastcgiphp

提问by andre487

I have some error with subj. Server doesn't high loaded: ~15% CPU, there are several Gb of memory, HDD is not buisy. But error 502 throws approximately in 3% of cases.

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

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

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

In error.log of nginx is this error:

在 nginx 的 error.log 中是这个错误:

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

State of php5-fpm usually like this:

php5-fpm 的状态通常是这样的:

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

I think, this mean loading is not high.

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

I have increased backlog params: in sysctl - net.core.somaxconn = 5000, in php-fpm pool - listen.backlog = 5000. No effect.

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

I quote a configuration:

我引用一个配置:

/etc/nginx/nginx.conf

/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

/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

php-fpm 池

[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

What can I do to optimize this system and make this use all server resources?

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

PS. I'm sorry, my english is bad.

附注。对不起,我的英语不好。

回答by Dmitry Verhoturov

The issue is socket itself, its problems on high-load cases is well-known. Please consider using TCP\IP connection instead of unix socket, for that you need to make these changes:

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

  • in php-fpm pool configurationreplace listen = /var/run/php5-fpm.sockwith listen = 127.0.0.1:7777
  • in /etc/nginx/php_locationreplace fastcgi_pass unix:/var/run/php5-fpm.sock;with fastcgi_pass 127.0.0.1:7777;
  • 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;

回答by Nibbels

On Centos 7, Plesk 12.5

在 Centos 7、Plesk 12.5 上

I had this problem after my harddisc went full and some services failed. Other Domains workt perfectly, but not one of them it only gave me 502 and similar as timeouts. From the Errorlog:

在我的硬盘已满并且某些服务失败后,我遇到了这个问题。其他域工作得很好,但不是其中之一,它只给了我 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

To solve it, I had to (first make space available and then) restart php-fpm and nginx - then this error vanished!

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

回答by Rohit Gupta

The only reason for this file not created is configuration at /etc/php-fpm.d/www.conf

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

Change listen = 127.0.0.1:9000

更改监听 = 127.0.0.1:9000

With listen = /var/run/php-fpm/php-fpm.sock

随着listen = /var/run/php-fpm/php-fpm.sock

And then restart both nginx and php-fpm

然后重启nginx和php-fpm

回答by New Alexandria

I the same issue, but didn't wish to switch from sockets to TCP/IP. Restarting php-fpm and nginx will solve the issue.

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

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