22

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.

Mohammad Jolani
  • 333
  • 1
  • 2
  • 8

3 Answers3

8

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.

Mark Stosberg
  • 3,771
  • 23
  • 27
4

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

user3520245
  • 140
  • 4
  • Having looked at this a bit more, the Python server this was hooked up to was giving back bad headers. I didn't bother fixing them as I didn't care enough about the service. – user3520245 Apr 20 '20 at 15:34
-3

Rename the upstream to "up_example.com" and change

proxy_pass  http://$server_name/$uri;

be

proxy_pass  http://up_$server_name$uri;
Mark R.
  • 365
  • 1
  • 5
  • While unconventional, I see no technical problem with the use of `$server_name`. If this was the problem, I think he would have problems /all/ the time, not just sometimes as reported. – Mark Stosberg Jan 25 '16 at 17:53