nginx:连接上游时没有活上游

在站点页面之间切换时显示502错误的网关错误,而在主页面上的某些时候会显示错误网关错误,但是只有在另一个页面redirect到主页面时才会出现错误。 它发生了一些JavaScript文件

两个上游configuration的负载均衡php1 php2都是apache服务器。

当我检查错误日志我喜欢:

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/" 

这是负载平衡服务器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; } } 

我search了几个小时,没有什么有用的发现,我的stream是起来,没有问题与他们。

这不是Nginx的问题,它是你的PHP后端没有及时响应的问题。 您可以将日志logging添加到Nginx以帮助确认这一点 。

作为第二个参考点,您可以在服务器top手动检查PHP是否在一段时间内closuresCPU,这是另一个响应速度慢的指标。

如果PHP的响应速度非常慢,那么您可以要求Nginx在放弃之前等待更长的时间:

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

通过检查与上面链接的时间信息的日志,你应该能够找出PHP处理缓慢的请求。

为了缩小问题的范围,请将这些请求直接发送到PHP后端。

根据发生的情况,你也可以在Nginx中启用一些请求的caching,避免一些缓慢的请求。

将上游重命名为“up_example.com”并更改

 proxy_pass http://$server_name/$uri; 

 proxy_pass http://up_$server_name$uri;