nginx:连接到上游时没有实时上游

Moh*_*ani 19 nginx gateway 502

在站点页面和主页上的某些时间之间切换时显示 502 错误网关错误,但不是主页上的第一个请求,只有在另一个页面重定向到它时才会显示。它发生在一些 javascript 文件中

在两个上游配置的负载均衡 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/"
Run Code Online (Sandbox Code Playgroud)

这是负载平衡服务器配置

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

    }
Run Code Online (Sandbox Code Playgroud)

我搜索了几个小时,没有任何帮助发现我的流已经启动并且它们没有问题。

Mar*_*erg 7

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

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

如果 PHP 响应很慢,你可以接受,你可以让 Nginx 在放弃之前等待更长时间:

 # Wait 5 minutes before giving up on the backend!
 proxy_read_timeout 5m; 
Run Code Online (Sandbox Code Playgroud)

通过检查带有上面链接的时间信息的日志,您应该能够确定 PHP 处理哪些请求很慢。

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

根据发生的情况,您可能还可以在 Nginx 中启用某些请求的缓存,从而避免一些缓慢的请求。


小智 -4

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

proxy_pass  http://$server_name/$uri;
Run Code Online (Sandbox Code Playgroud)

proxy_pass  http://up_$server_name$uri;
Run Code Online (Sandbox Code Playgroud)