即使服务器仍处于活动状态,nginx proxy_pass最终也会返回GATEWAY_TIMEOUT


9

我已经设置了一个nginx代理传递,当nginx最初加载时它可以正常工作,但是最终(也许是一天后?还是不太确定),作为代理传递的位置只会返回504 GATEWAY_TIMEOUT响应。奇怪的是,我已经设置了多个proxy_pass位置,出现下降和保持上升是随机的。

这是nginx配置文件:

server {
  listen   80;
  server_name  example.com;
  access_log  /var/log/nginx/access.log;
  error_log  /var/log/nginx/error.log;
  root   /srv/www/frontend/current/;

  location / {
    try_files $uri $uri/ /index.html;
  }

  # Block all svn access
  if ($request_uri ~* ^.*\.svn.*$) {
     return 404;
  }

  # Block all git access
  if ($request_uri ~* ^.*\.git.*$) {
     return 404;
  }

  location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
  }

  location /api/karma/ {
    proxy_pass <server A>;
  }

  location /api/auth/ {
    proxy_pass <server B>;
  }

  location /api/search/ {
    proxy_pass <server C>;
  }

  location /api/history/ {
    proxy_pass <server D>;
  }
}

(显然,我已经将服务器位置匿名了)

例如,当nginx最初启动时,我可以

curl -i example.com/api/history/health_check

(其中health_check是已知的端点),并获得200响应,但过一会儿,相同的确切命令将返回504 GATEWAY_TIMEOUT。

底层服务器和http服务器仍然可以完全正常运行,我可以直接对服务器地址执行curl命令,并从本地计算机以及将其放入Nginx服务器时获得响应。

开始返回GATEWAY_TIMEOUT的位置前缀是随机的,并且自从设置了该位置前缀以来,四个具有proxy_pass的位置中的每一个都以这种方式在某一点或另一点失败。

一切都托管在AWS t2_micro Ubuntu 14.04 Linux实例上,nginx本身是v1.4.6。基础服务器/应用程序不会重新启动,AWS实例本身也一直处于运行状态。底层的http服务器是Go应用程序,但是同样,当我开始获取这些GATEWAY_TIMEOUT时,这些应用程序本身就可以完全运行。

发生这种情况时,错误日志中没有什么有趣的东西,但是我刚刚打开了错误日志中的调试日志,这是一个坏请求的结果:

2015/07/13 19:56:31 [debug] 3523#0: *12096 post event 00000000022542A0
2015/07/13 19:56:31 [debug] 3523#0: *12096 delete posted event 00000000022542A0
2015/07/13 19:56:31 [debug] 3523#0: *12096 http wait request handler
2015/07/13 19:56:31 [debug] 3523#0: *12096 posix_memalign: 000000000222F6A0:256 @16
2015/07/13 19:56:31 [debug] 3523#0: *12096 malloc: 0000000002225CA0:1024
2015/07/13 19:56:31 [debug] 3523#0: *12096 recv: fd:23 1018 of 1024
2015/07/13 19:56:31 [debug] 3523#0: *12096 reusable connection: 0
2015/07/13 19:56:31 [debug] 3523#0: *12096 posix_memalign: 00000000022260B0:4096 @16
2015/07/13 19:56:31 [debug] 3523#0: *12096 http process request line
2015/07/13 19:56:31 [debug] 3523#0: *12096 http request line: "POST <url> HTTP/1.1"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http uri: "<url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http args: ""
2015/07/13 19:56:31 [debug] 3523#0: *12096 http exten: ""
2015/07/13 19:56:31 [debug] 3523#0: *12096 http process request header line
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "host: <url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "Accept: application/json, text/plain, */*"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "Accept-Encoding: gzip, deflate"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "Accept-Language: en-US,en;q=0.8"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "Authorization: <auth token>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "Content-Type: application/json;charset=UTF-8"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "Origin: <url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "Referer: <url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "X-Forwarded-For: <ip address>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "X-Forwarded-Port: 443"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "X-Forwarded-Proto: https"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "Content-Length: 209"
2015/07/13 19:56:31 [debug] 3523#0: *12096 posix_memalign: 00000000022270C0:4096 @16
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header: "Connection: keep-alive"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http header done
2015/07/13 19:56:31 [debug] 3523#0: *12096 event timer del: 23: 1436817428740
2015/07/13 19:56:31 [debug] 3523#0: *12096 generic phase: 0
2015/07/13 19:56:31 [debug] 3523#0: *12096 rewrite phase: 1

** snip ( bunch of logs about trying to match the url to a route )

2015/07/13 19:56:31 [debug] 3523#0: *12096 http cl:209 max:4194304
2015/07/13 19:56:31 [debug] 3523#0: *12096 rewrite phase: 3
2015/07/13 19:56:31 [debug] 3523#0: *12096 post rewrite phase: 4
2015/07/13 19:56:31 [debug] 3523#0: *12096 generic phase: 5
2015/07/13 19:56:31 [debug] 3523#0: *12096 generic phase: 6
2015/07/13 19:56:31 [debug] 3523#0: *12096 generic phase: 7
2015/07/13 19:56:31 [debug] 3523#0: *12096 access phase: 8
2015/07/13 19:56:31 [debug] 3523#0: *12096 access phase: 9
2015/07/13 19:56:31 [debug] 3523#0: *12096 post access phase: 10
2015/07/13 19:56:31 [debug] 3523#0: *12096 try files phase: 11
2015/07/13 19:56:31 [debug] 3523#0: *12096 http client request body preread 209
2015/07/13 19:56:31 [debug] 3523#0: *12096 http request body content length filter
2015/07/13 19:56:31 [debug] 3523#0: *12096 http body new buf t:1 f:0 0000000002225FC9, pos 0000000002225FC9, size: 209 file: 0, size: 0
2015/07/13 19:56:31 [debug] 3523#0: *12096 http init upstream, client timer: 0
2015/07/13 19:56:31 [debug] 3523#0: *12096 epoll add event: fd:23 op:3 ev:80000005
2015/07/13 19:56:31 [debug] 3523#0: *12096 http script copy: "Host: "
2015/07/13 19:56:31 [debug] 3523#0: *12096 http script var: "<aws url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http script copy: "
"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http script copy: "Connection: close
"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http script copy: "Content-Length: "
2015/07/13 19:56:31 [debug] 3523#0: *12096 http script var: "209"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http script copy: "
"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Accept: application/json, text/plain, */*"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Accept-Encoding: gzip, deflate"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Accept-Language: en-US,en;q=0.8"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Authorization: <auth>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Content-Type: application/json;charset=UTF-8"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Origin: <url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Referer: <url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "X-Forwarded-For: 96.45.202.230"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "X-Forwarded-Port: 443"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "X-Forwarded-Proto: https"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header:
"POST /v1/trip HTTP/1.0
Host: <url>
Connection: close
Content-Length: 209
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Authorization: <auth>
Content-Type: application/json;charset=UTF-8
Origin: <url>
Referer: <url>
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36
X-Forwarded-For: 96.45.202.230
X-Forwarded-Port: 443
X-Forwarded-Proto: https

"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http cleanup add: 0000000002227C40
2015/07/13 19:56:31 [debug] 3523#0: *12096 get rr peer, try: 2
2015/07/13 19:56:31 [debug] 3523#0: *12096 get rr peer, current: 1 0
2015/07/13 19:56:31 [debug] 3523#0: *12096 socket 24
2015/07/13 19:56:31 [debug] 3523#0: *12096 epoll add connection: fd:24 ev:80000005
2015/07/13 19:56:31 [debug] 3523#0: *12096 connect to 54.174.36.245:80, fd:24 #12097
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream connect: -2
2015/07/13 19:56:31 [debug] 3523#0: *12096 posix_memalign: 000000000222F7B0:128 @16
2015/07/13 19:56:31 [debug] 3523#0: *12096 event timer add: 24: 60000:1436817451902
2015/07/13 19:56:31 [debug] 3523#0: *12096 http finalize request: -4, "<url>" a:1, c:2
2015/07/13 19:56:31 [debug] 3523#0: *12096 http request count:2 blk:0
2015/07/13 19:56:31 [debug] 3523#0: *12096 post event 000000000226E2B0
2015/07/13 19:56:31 [debug] 3523#0: *12096 delete posted event 000000000226E2B0
2015/07/13 19:56:31 [debug] 3523#0: *12096 http run request: "<url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream check client, write event:1, "<url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream recv(): -1 (11: Resource temporarily unavailable)
2015/07/13 19:56:31 [debug] 3523#0: post event 0000000002253E90
2015/07/13 19:56:31 [debug] 3523#0: delete posted event 0000000002253E90
2015/07/13 19:56:31 [debug] 3523#0: accept on 0.0.0.0:80, ready: 0
2015/07/13 19:56:31 [debug] 3523#0: posix_memalign: 000000000222F840:256 @16
2015/07/13 19:56:31 [debug] 3523#0: *12098 accept: 172.31.15.160 fd:25
2015/07/13 19:56:31 [debug] 3523#0: *12098 event timer add: 25: 60000:1436817451902
2015/07/13 19:56:31 [debug] 3523#0: *12098 reusable connection: 1
2015/07/13 19:56:31 [debug] 3523#0: *12098 epoll add event: fd:25 op:1 ev:80000001
2015/07/13 19:56:31 [debug] 3523#0: post event 0000000002253E90
2015/07/13 19:56:31 [debug] 3523#0: delete posted event 0000000002253E90
2015/07/13 19:56:31 [debug] 3523#0: accept on 0.0.0.0:80, ready: 0
2015/07/13 19:56:31 [debug] 3523#0: posix_memalign: 000000000222F950:256 @16
2015/07/13 19:56:31 [debug] 3523#0: *12099 accept: 172.31.15.160 fd:26
2015/07/13 19:56:31 [debug] 3523#0: *12099 event timer add: 26: 60000:1436817451902
2015/07/13 19:56:31 [debug] 3523#0: *12099 reusable connection: 1
2015/07/13 19:56:31 [debug] 3523#0: *12099 epoll add event: fd:26 op:1 ev:80000001
2015/07/13 19:56:31 [debug] 3523#0: *12096 post event 000000000226E040
2015/07/13 19:56:31 [debug] 3523#0: *12096 delete posted event 000000000226E040
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream request: "<url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream send request handler
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream send request
2015/07/13 19:56:31 [debug] 3523#0: *12096 chain writer buf fl:0 s:830
2015/07/13 19:56:31 [debug] 3523#0: *12096 chain writer buf fl:1 s:209
2015/07/13 19:56:31 [debug] 3523#0: *12096 chain writer in: 0000000002227C98
2015/07/13 19:56:31 [debug] 3523#0: *12096 writev: 1039
2015/07/13 19:56:31 [debug] 3523#0: *12096 chain writer out: 0000000000000000
2015/07/13 19:56:31 [debug] 3523#0: *12096 event timer del: 24: 1436817451902
2015/07/13 19:56:31 [debug] 3523#0: *12096 event timer add: 24: 60000:1436817451904
2015/07/13 19:56:31 [debug] 3523#0: *12096 post event 0000000002254030
2015/07/13 19:56:31 [debug] 3523#0: *12096 post event 000000000226E040
2015/07/13 19:56:31 [debug] 3523#0: *12096 delete posted event 000000000226E040
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream request: "<url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream dummy handler
2015/07/13 19:56:31 [debug] 3523#0: *12096 delete posted event 0000000002254030
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream request: "<url>"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream process header
2015/07/13 19:56:31 [debug] 3523#0: *12096 malloc: 00000000022280D0:4096
2015/07/13 19:56:31 [debug] 3523#0: *12096 recv: fd:24 141 of 4096
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy status 200 "200 OK"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Content-Type: text/plain"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Date: Mon, 13 Jul 2015 19:56:31 GMT"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Server: nginx/1.6.2"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Content-Length: 0"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header: "Connection: Close"
2015/07/13 19:56:31 [debug] 3523#0: *12096 http proxy header done
2015/07/13 19:56:31 [debug] 3523#0: *12096 xslt filter header
2015/07/13 19:56:31 [debug] 3523#0: *12096 HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Mon, 13 Jul 2015 19:56:31 GMT
Content-Type: text/plain
Content-Length: 0
Connection: keep-alive

(对我而言)有趣的一行是:

2015/07/13 19:56:31 [debug] 3523#0: *12096 http upstream recv(): -1 (11: Resource temporarily unavailable)

我尝试在Google中查找该字符串,但老实说,我不太了解答案,而且它们似乎与我遇到的任何问题都不相关。


nginx使用非阻塞I / O。您所指的这个错误是完全正常的,它只是告诉nginx此时无法从该套接字读取任何内容。
奥利弗(Oliver)

Answers:


1

瓶颈可能在您的t2_micro文件描述符数目中,但也很有可能是网络带宽问题。

有两种解决此问题的通用方法,而又不增加带宽:1)增加系统范围内打开文件的限制(数量)和/或nginx的ulimit 2)升级到nginx 1.7.5或更高版本

如果增加文件描述符不能解决问题,请记住t2_micro通常是低网络带宽产品。尽管您可能无法获得“更好”的网络性能,但是您可以尝试通过两次调整来尝试“更准确”的代理响应health_check,以最大程度地减少t2_micro的缺点:

  • 默认情况下,缓冲处于打开状态,因此请确认中指定的缺省路径proxy_temp_path存在并且具有足够的磁盘空间,因为如果不存在,缓冲的消息可能偶尔会为空或丢失,并创建超时条件以等待响应。
  • 您将需要版本1.7.5或更高版本来进行调整,proxy_next_upstream_timeout并使proxy_next_upstream_triesnginx在低带宽服务器上“更智能”地处理代理请求。例如,您可以定义多个重试,然后再传递错误。

更新:要配置版本1.7.5及更高版本中出现错误之前的代理尝试次数,请将此条目添加到nginx.conf中:

proxy_next_upstream_tries 3;

这将给您两次重试(一次尝试,再两次重试)。


凉!尝试第二个选项(升级到最新的Nginx稳定版本),看看会发生什么。通常情况下,问题触发需要一到两天,因此我将在结果发布后在此处发布回复。
syazdani

太棒了 本身升级到1.7.5可能无法解决问题。由于默认的proxy_retries为0,因此您必须编辑nginx.conf并添加带有数字的数字,例如1或2。
Andrew S

我用proxy_next_upsteam_tries选项的格式更新了我的答案
Andrew S

因此,我实际上只是升级到了nginx 1.8.0,大约一周来我们就一直没有问题。这并不意味着它100%消失了,但是对我而言已经足够了。
syazdani

1

将此添加到nginx.conf:

proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;

然后执行'service nginx reload'(如果使用systemd,则执行'systemctl nginx reload')

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.