46

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:

/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

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

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

andre487
  • 1,261
  • 2
  • 18
  • 27
  • 2
    I'm on Ubuntu 13.10 and the logging of php5-fpm is somehow misrepresenting what happens... actually, the logs said nothing :) I found the error by running the daemon directly from command line instead, `sudo php5-fpm --daemonize --fpm-config /etc/php5/fpm/php-fpm.conf` – benjaoming Jan 11 '14 at 14:09
  • 7
    just in case someone is searching for php-fpm pool file its in /etc/php5/fpm/pool.d/www.conf on Ubuntu 12.04.3 LTS – Kristóf Dombi May 10 '14 at 08:28
  • For those who come to this question by googling: Try this solution first: http://stackoverflow.com/questions/23443398/nginx-error-connect-to-php5-fpm-sock-failed-13-permission-denied – Alexar Jul 10 '14 at 04:01

4 Answers4

107

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:

  • in php-fpm pool configuration replace listen = /var/run/php5-fpm.sock with listen = 127.0.0.1:7777
  • in /etc/nginx/php_location replace fastcgi_pass unix:/var/run/php5-fpm.sock; with fastcgi_pass 127.0.0.1:7777;
Dmitry Verhoturov
  • 5,835
  • 2
  • 32
  • 39
  • Previously, it was just like this - TCP/IP socket was used. But the situation was the same. I'll try TCP/IP socket again - will see, what it gives us. – andre487 May 06 '12 at 13:28
  • 2
    Now is much better. In addition I want to say that update PHP to version 5.4 has very good effect. – andre487 May 06 '12 at 15:45
  • Yes, it's gone. nginx is updated to version 1.1.19. I thought about caching on nginx, but in current application it will not useful. – andre487 May 08 '12 at 20:51
  • 4
    I think there's a mistake here. The `=` sign is missing. It should be `listen = 127.0.0.1:7777`, and not `listen 127.0.0.1:7777` AFAIK. – its_me Feb 13 '13 at 19:47
  • It works. Very strange that it appeared to be an issue on PHP 5.4, but was all right with 5.3. – Anton Babenko Sep 09 '13 at 11:30
  • "moving to TCP/IP forced the PHP requests to slow down (instead of 502). This was preferable. (from a deleted answer that we found useful). – RedBlueThing Dec 20 '13 at 07:03
  • I don't seem to be able to find `/etc/nginx/php_location` on Ubuntu 14.04.3 LTS - any idea where it could be found? – Sebastian Sulinski Sep 10 '15 at 13:56
  • 2
    Found it - it's now within the `/etc/nginx/sites-available/*` – Sebastian Sulinski Sep 10 '15 at 14:00
  • I think the Unix socket performance is better than TCP socket. Why change back to TCP for high-load cases? – northtree Feb 10 '16 at 01:15
  • 4
    My explanation of why this works is proven wrong by my colleagues but it still seems somewhy working for many people. Years later I'm still searching what can be the root cause of the problem and can't find it. – Dmitry Verhoturov Feb 11 '16 at 05:44
  • 2
    On ubuntu location of the php-fpm pool configuration is /etc/php5/fpm/pool.d/www.conf also don't forget to restart both nginx and php5-fpm services after you make those changes. – Tamik Soziev Mar 31 '16 at 06:21
2

On 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:

[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!

Nibbels
  • 21
  • 1
0

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

Change listen = 127.0.0.1:9000

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

And then restart both nginx and php-fpm

Rohit Gupta
  • 61
  • 1
  • 7
  • I have that line, restarted both but the file is still not there. No errors in php5-fpm.log – Alecz Oct 03 '17 at 16:31
-6

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

sudo /etc/init.d/php-fpm restart
sudo /etc/init.d/nginx restart
New Alexandria
  • 6,951
  • 4
  • 57
  • 77
  • 2
    Restarting the application whenever you face a issue, can't ever be considered a solution. – Napster_X Mar 30 '15 at 08:35
  • Depending upon the frequency of the problem, and the amount of resources allocatable – yes, it can be a solution. The fundamental point that *systems should be better designed* does remain, of course – New Alexandria Mar 30 '15 at 18:25
  • @GeekRide if the situation demanded the use of a socket connection, then as DmitryV stated "its problems on high-load cases is well-known", we would have no other choice but to strategically reboot the service. I think its sad that so many people overlook this because they're working in environments with lower production costs. – New Alexandria May 27 '15 at 03:54
  • 1
    +1 for the suggestion, in my case investigating the status of service in question `sudo service php5-fpm status` i noticed it was somehow not running; so by simply starting (or restarting it) `sudo service php5-fpm start` solved the problem. – Luca Filosofi May 26 '16 at 09:09
  • I want to leave this here for historic reference — i.e. in that it is another strategy, albeit inferior to the accepted answer. Acknowledging the remnant purpose of this answer, I'd like to ask people stop downvoting it. I would delete it, except that others might think this, too, is a viable alternative to the accepted answer. – New Alexandria Feb 27 '17 at 22:45