nginx : no live upstreams while connecting to upstream

502 bad gateway error displayed when switching between site pages and some times on home page but not for the first request on the home page it is only when another page redirect to it. and it happens for some javascript files

load balancing configured on two upstreams php1 php2 both are apache server.

When I checked error log i fond:

no live upstreams while connecting to upstream

[error] 27212#0: *314 no live upstreams while connecting to   upstream, client: ip_address , server: example.com, request: "GET / HTTP/1.1", upstream: "http://example.com", host: "example.com", referrer: "http://example.com/mypages/"

and this is load balancing server configuration

  upstream example.com  {
    #  ip_hash;
      server php01 max_fails=3 fail_timeout=15s;
      server php02 max_fails=3 fail_timeout=15s;
    }

    server {
      listen IP:80;
      server_name example.com;
      access_log /var/log/nginx/example.com.access;
      error_log /var/log/nginx/example.com.error error;

     location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass  http://$server_name/$uri;
        proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
        proxy_cache_bypass $http_pragma $http_authorization;
        proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
        proxy_no_cache $http_pragma $http_authorization;
      }

    }

I searched for hours and nothing helpful found my streams are up and no problems with them.


This is not a problem with Nginx, it is a problem with your PHP backends not responding in time. You can add logging to Nginx to help confirm this.

As a second point of reference, you can you can top on the server and manually check if PHP is slamming the CPU for periods of time, another indicator of slow responses.

If very slow responses from PHP are OK with you, you can ask Nginx to wait longer before giving up:

 # Wait 5 minutes before giving up on the backend!
 proxy_read_timeout 5m; 

By examining the logs with the timing information linked to above, you ought to be able to figure out which requests are slow for PHP to process.

To narrow down the problem, send these requests directly to the PHP backend.

Depending on what's happening, you might also be able enable caching of some requests in Nginx, avoiding some of slow requests.


Don't know if it is quite the same but what worked for me was to add max_fails = 0 to the end of the server name

upstream sm_url { server LOAD_BALANCER_DOMAIN_NAME: max_fails=0; }