Solve recv() failed (104: Connection reset by peer) 502 NGINX Bad Gateway PHP5 FPM


When I host many wordpress sites under NGINX + PHP5-FPM on Ubuntu Oneiric, everything is smooth and there no problem. Suddenly, after a few month, I got error NGINX 502 Gateway on my cron. Then after dive into NGINX error.log, I see this error :

1
2
2012/03/19 14:20:21 [error] 2383#0: *334 readv() failed (104: Connection reset by peer) while reading upstream, client: 199….., server: www.clipsta.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "www.clipsta.com"
2012/03/19 14:21:18 [error] 2899#0: *19 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 103.2…, server: www.clipsta.com, request: "GET /wp-cron.php?doing_wp_cron HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "www.clipsta.com"


So, I thought it was NGINX timeout problem. Here are some sample of my NGINX :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
client_body_timeout  1460;
client_header_timeout 1460;
send_timeout 1460;
client_max_body_size 10m;
keepalive_timeout 1300;

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
     fastcgi_send_timeout  5m;
     fastcgi_read_timeout 5m;
     fastcgi_connect_timeout 5m;
     fastcgi_pass unix:/var/run/php5-fpm.sock;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     include fastcgi_params;
}

Then, I know the problem come in PHP-FPM, so I edit “/etc/php5/fpm/pool.d/www.conf” and I see the solution is by increasing backlog and optimize several options :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/slowlog-blog.log
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 4
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 200
listen.backlog = -1
pm.status_path = /status
request_terminate_timeout = 120s
rlimit_files = 131072
rlimit_core = unlimited

Now everything goes well again 🙂


One response to “Solve recv() failed (104: Connection reset by peer) 502 NGINX Bad Gateway PHP5 FPM”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.