ในที่สุด nginx proxy_pass จะส่งคืน GATEWAY_TIMEOUT แม้ว่าเซิร์ฟเวอร์จะยังทำงานอยู่


9

ฉันได้ตั้งค่าพร็อกซี nginx passthrough ซึ่งทำงานได้ดีเมื่อเริ่มโหลด nginx แต่ในที่สุด (ในที่สุดอาจจะหลังจากผ่านไปหนึ่งวันแล้วหรือไม่? ไม่แน่ใจว่า ณ จุดนี้) ตำแหน่งที่เป็นพร็อกซี passthrough จะส่งคืนการ504 GATEWAY_TIMEOUTตอบกลับเท่านั้น สิ่งที่แปลกคือฉันได้ตั้งหลาย proxy_pass ที่ตั้งและเป็นสิ่งที่สุ่มลงไปและสิ่งที่ยังคงอยู่

นี่คือไฟล์ config 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 กับที่อยู่ของเซิร์ฟเวอร์โดยตรงและรับการตอบสนองจากเครื่องของฉันและเมื่อ sshed เข้าสู่เซิร์ฟเวอร์ 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

คำตอบ:


1

คอขวดน่าจะเป็นจำนวนตัวอธิบายไฟล์ t2_micro ของคุณ แต่ก็มีความเป็นไปได้สูงที่จะมีปัญหาแบนด์วิดท์เครือข่าย

มีวิธีทั่วไปสองวิธีในการแก้ไขปัญหานี้โดยไม่เพิ่มแบนด์วิดท์: 1) เพิ่มขีด จำกัด (จำนวน) ของระบบไฟล์ที่เปิดกว้างและ / หรือ ulimit สำหรับ nginx 2) อัปเกรดเป็น nginx 1.7.5 หรือสูงกว่า

หากตัวอธิบายไฟล์ที่เพิ่มขึ้นไม่สามารถแก้ไขปัญหาได้โปรดจำไว้ว่า t2_micro เป็นผลิตภัณฑ์แบนด์วิธเครือข่ายที่ต่ำ แม้ว่าคุณอาจไม่สามารถรับประสิทธิภาพเครือข่ายที่ 'ดีกว่า' ได้ แต่คุณสามารถลองใช้พร็อกซีการตอบสนองที่แม่นยำยิ่งขึ้น health_check ที่ช่วยลดจุดอ่อนของ t2_micro โดยใช้การปรับแต่งสองอย่าง:

  • การบัฟเฟอร์เปิดอยู่ตามค่าเริ่มต้นดังนั้นให้ยืนยันเส้นทางเริ่มต้นที่ระบุในที่proxy_temp_pathมีอยู่และมีพื้นที่ว่างในดิสก์เพียงพอเพราะหากไม่เป็นเช่นนั้นข้อความที่บัฟเฟอร์อาจจะว่างเปล่าหรือพลาดเป็นระยะ ๆ และสร้างเงื่อนไขการหมดเวลารอการตอบกลับ
  • คุณจะต้องใช้เวอร์ชัน 1.7.5 ขึ้นไปเพื่อปรับproxy_next_upstream_timeoutและproxy_next_upstream_triesให้ nginx จัดการคำขอพร็อกซี 'ฉลาดขึ้น' บนเซิร์ฟเวอร์แบนด์วิดธ์ต่ำ ตัวอย่างเช่นคุณสามารถกำหนดจำนวนครั้งในการลองอีกครั้งก่อนส่งข้อผิดพลาด

UPDATE: หากต้องการกำหนดจำนวนพร็อกซีให้ลองก่อนเกิดข้อผิดพลาดในรุ่น 1.7.5 ขึ้นไปให้เพิ่มรายการนี้ใน nginx.conf:

proxy_next_upstream_tries 3;

วิธีนี้จะให้คุณลองสองครั้ง(ลองครั้งเดียวและลองอีกสองครั้ง)


เย็น! ลองใช้ตัวเลือกที่สอง (อัปเกรดเป็นรุ่นล่าสุดที่เสถียรของ nginx) และดูว่าเกิดอะไรขึ้น โดยทั่วไปแล้วจะใช้เวลาหนึ่งหรือสองวันก่อนที่ปัญหาจะเริ่มต้นดังนั้นฉันจะโพสต์คำตอบที่นี่หลังจากผลลัพธ์
syazdani

น่ากลัว การอัปเกรดเป็น 1.7.5 โดยตัวมันเองอาจไม่สามารถแก้ไขปัญหาได้ เนื่องจากค่าเริ่มต้น proxy_retries คือ 0 คุณจะต้องแก้ไข nginx.conf และเพิ่มพารามิเตอร์ด้วยตัวเลขเช่น 1 หรือ 2 ยกนิ้วให้ช่วย แต่ให้ OP รู้เสมอว่าคุณทำอะไรเพื่อแก้ไขปัญหาในที่สุด
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' ('systemctl nginx reload' หากคุณใช้ systemd)

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.