调试功能

5xx 响应状态码

500、502、503 等类似的 5xx 状态码,是由于服务器错误而响应的状态码,当一个请求出现 5xx 状态码时;它可能来源于 APISIXUpstream 。如何识别这些响应状态码的来源,是一件很有意义的事,它能够快速的帮助我们确定问题的所在。(当修改 conf/config.yaml 的配置 show_upstream_status_in_response_headertrue 时,会返回所有上游状态码,不仅仅是 5xx 状态。)

如何识别 5xx 响应状态码的来源

在请求的响应头中,通过 X-APISIX-Upstream-Status 这个响应头,我们可以有效的识别 5xx 状态码的来源。当 5xx 状态码来源于 Upstream 时,在响应头中可以看到 X-APISIX-Upstream-Status 这个响应头,并且这个响应头的值为响应的状态码。当 5xx 状态码来源于 APISIX 时,响应头中没有 X-APISIX-Upstream-Status 的响应头信息。也就是只有 5xx 状态码来源于 Upstream 时,才会有 X-APISIX-Upstream-Status 响应头。

示例

示例 1:502 响应状态码来源于 Upstream (IP 地址不可用)

  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "upstream": {
  5. "nodes": {
  6. "127.0.0.1:1": 1
  7. },
  8. "type": "roundrobin"
  9. },
  10. "uri": "/hello"
  11. }'

测试:

  1. $ curl http://127.0.0.1:9080/hello -v
  2. ......
  3. < HTTP/1.1 502 Bad Gateway
  4. < Date: Wed, 25 Nov 2020 14:40:22 GMT
  5. < Content-Type: text/html; charset=utf-8
  6. < Content-Length: 154
  7. < Connection: keep-alive
  8. < Server: APISIX/2.0
  9. < X-APISIX-Upstream-Status: 502
  10. <
  11. <html>
  12. <head><title>502 Bad Gateway</title></head>
  13. <body>
  14. <center><h1>502 Bad Gateway</h1></center>
  15. <hr><center>openresty</center>
  16. </body>
  17. </html>

具有 X-APISIX-Upstream-Status: 502 的响应头。

示例 2:502 响应状态码来源于 APISIX

  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "abort": {
  6. "http_status": 500,
  7. "body": "Fault Injection!\n"
  8. }
  9. }
  10. },
  11. "uri": "/hello"
  12. }'

测试:

  1. $ curl http://127.0.0.1:9080/hello -v
  2. ......
  3. < HTTP/1.1 500 Internal Server Error
  4. < Date: Wed, 25 Nov 2020 14:50:20 GMT
  5. < Content-Type: text/plain; charset=utf-8
  6. < Transfer-Encoding: chunked
  7. < Connection: keep-alive
  8. < Server: APISIX/2.0
  9. <
  10. Fault Injection!

没有 X-APISIX-Upstream-Status 的响应头。

示例 3:Upstream 具有多节点,并且所有节点不可用

  1. $ curl http://127.0.0.1:9180/apisix/admin/upstreams/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "nodes": {
  4. "127.0.0.3:1": 1,
  5. "127.0.0.2:1": 1,
  6. "127.0.0.1:1": 1
  7. },
  8. "retries": 2,
  9. "type": "roundrobin"
  10. }'
  1. $ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/hello",
  4. "upstream_id": "1"
  5. }'

测试:

  1. $ curl http://127.0.0.1:9080/hello -v
  2. < HTTP/1.1 502 Bad Gateway
  3. < Date: Wed, 25 Nov 2020 15:07:34 GMT
  4. < Content-Type: text/html; charset=utf-8
  5. < Content-Length: 154
  6. < Connection: keep-alive
  7. < Server: APISIX/2.0
  8. < X-APISIX-Upstream-Status: 502, 502, 502
  9. <
  10. <html>
  11. <head><title>502 Bad Gateway</title></head>
  12. <body>
  13. <center><h1>502 Bad Gateway</h1></center>
  14. <hr><center>openresty</center>
  15. </body>
  16. </html>

具有 X-APISIX-Upstream-Status: 502, 502, 502 的响应头。