2

When I open my site, I get a 502 bad gateway error. After looking at the error log file in /var/log/nginx/error.log, it mentions that "upstream prematurely closed connection while reading response header from upstream".

nginx.conf file content:

#user  nginx;
worker_processes  1;

#error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

#pid        /var/run/nginx.pid;


events {
    worker_connections  10240;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

   #access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    client_header_timeout 600;
    client_body_timeout 600;
    send_timeout 6000;
    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;
    keepalive_timeout  36500;
    proxy_read_timeout 1250000;
    proxy_connect_timeout 1250000;
    fastcgi_read_timeout 1250000;
    #tcp_nodelay        on;

    #gzip  on;
    #gzip_disable "MSIE [1-6]\.(?!.*SV1)";


  server_tokens off;

    include /etc/nginx/conf.d/*.conf;

How to solve this?

  • it's often due to unresponsive element you set in your config file, like static server could you ad a bit more details – Kiwy Mar 25 '14 at 12:18
  • Could you add your /etc/nginx/nginx.conf to your question? – Braiam Mar 27 '14 at 17:22
  • Please add any files you've edited in /etc/nginx/conf.d as well. – Flup Mar 28 '14 at 8:11
1

I had the same issue but due to the rise of audience in our site, at first i did all suggestions i can find here "How to fix gateway timeout?" and other sites and here in SO, and still had the same issue of 502 gateway error. So i caught the issue that the server i was working has a website and an apiend both in the same server, so i transferred the apiend onto another server which worked very well, until we got a lot more requests as our clients having more and more events and 502 gateway timeout issue came back, the apiend is still responding while the website is on 502.

So i tried checking all my server logs, until i checked my php-fpm.log and saw this errors:

[26-Mar-2015 10:10:04] WARNING: [pool www] server reached pm.max_children setting (35), consider raising it
[26-Mar-2015 12:04:52] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers)

As it said in my php-fpm.log i need to increase the max_children setting, on my /etc/php5/fpm/pool.d/www.conf my current setting is:

pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 4
pm.max_spare_servers = 8

Using this command: ps -ylC php5-fpm --sort:rss i checked for the average memory usage in kilobytes per process of php-fpm then compute for the pm.max_children.

Calculating pm.max_children

An example: if our cloud server has 4 GB RAM and a MySQL database service is running as well that consumes at least 1 GB our best aim is to get 4 - 1 - 0,5 (marge) GB = 2,5 GB RAM or 2560 Mb.

pm.max_children brings us to 2560 Mb / 60 Mb = 42 max_children

After i did this, it's been 8 months i haven't encountered the 502 gateway error and in also we checked our codes and found out that other developers code which were critical process were consuming a lot of resources so we still we made improvement on the backend codes as well.

Even if this posts is old, i just wanted to share how i resolved the issue. :D

Here is my reference. Determining the correct number of child processes for PHP-FPM on NGinx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.