Viewed   72 times

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.

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

In error.log of nginx is this error:

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

State of php5-fpm usually like this:

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.

I quote a configuration:


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/;

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;


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

listen = /var/run/php5-fpm.sock
listen.backlog = 5000
listen.owner = www-data = 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.



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

  • in php-fpm pool configuration replace listen = /var/run/php5-fpm.sock with listen =
  • in /etc/nginx/php_location replace fastcgi_pass unix:/var/run/php5-fpm.sock; with fastcgi_pass;
Friday, August 26, 2022

First php has to correctly flush everything :


Then, I found two working solutions:

1) Via Nginx configuration:

fastcgi_buffering off;

2) Via HTTP header in the php code

header('X-Accel-Buffering: no');
Monday, September 19, 2022

Make a file that you can include wherever you need it. For instance:

cat > /etc/nginx/php-fpm <<EOF
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;

Then modify your location to include php-fpm; and fix the root directive (it should appear under server, not under location /; this is a common nginx misconfiguration).

Friday, November 25, 2022

I believe I ran into exactly the same problem today, nginx does send the request to php-fpm (as indicated in the header) yet you get a 404, even though the file exists and has no (PHP/syntax) error at all, and no errors show up in any log (ngins or php-fpm).

You didn't include your full nginx config, but is it possible you don't have the option "root" define (correctly?) in your "server" section ? You need to make sure you do, that it points to the right location ofc and that it is inside the "server" section, not within a "location" one -- e.g:

server {
    root /var/www/eman;
    location ~ .php$ {
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/eman/$fastcgi_script_name;
        include fastcgi_params;
Thursday, December 1, 2022

It looks like your only option is to set fastcgi_read_timeout to a really big value (like 1h or 1d).

Wednesday, October 5, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :