没有不值得去解决的问题,也没有不值得去学习的技术!

Troubleshooting 404 and 504 Responses When Backend API Requests Time Out

When a frontend page requests a frontend interface and the response times out, it sometimes returns a 504 (as expected) and sometimes a 404 (unexpected).

1. When a frontend page requests a frontend interface and the response times out, it sometimes returns a 504 (as expected) and sometimes a 404 (unexpected). See Figure 1.

When a frontend page requests a frontend interface and the response times out, it sometimes returns a 504 (as expected) and sometimes a 404 (unexpected).
Figure 1
JSON

{
  "_request_time": "60.004",
  "_status": "404"
}

2. Querying the ingress-nginx logs based on _request_id:004c4f55c16c0904759fdb580d7c5df9, there is one record in ingress-nginx-controller (10-14 16:01:33) with a 404 response. See Figure 2.

Querying the ingress-nginx logs based on _request_id:004c4f55c16c0904759fdb580d7c5df9, there is one record in ingress-nginx-controller (10-14 16:01:33) with a 404 response.
Figure 2

3. Querying the Frontend Service logs based on _request_id:004c4f55c16c0904759fdb580d7c5df9, there is one record in Frontend Service (10-14 16:01:33) requesting /50x.html with a 404 response and _upstream_status:504, indicating that Backend Service responded with 504. See Figure 3.

Querying the Frontend Service logs based on _request_id:004c4f55c16c0904759fdb580d7c5df9, there is one record in Frontend Service (10-14 16:01:33) requesting /50x.html with a 404 response and _upstream_status:504, indicating that Backend Service responded with 504.
Figure 3

4. Querying the Backend Service logs based on _request_id:004c4f55c16c0904759fdb580d7c5df9, there is one record in Backend Service (10-14 16:01:33) with a 504 response; because Frontend Service actively disconnected, the actual recorded response is 499. See Figure 4.

Querying the Backend Service logs based on _request_id:004c4f55c16c0904759fdb580d7c5df9, there is one record in Backend Service (10-14 16:01:33) with a 504 response; because Frontend Service actively disconnected, the actual recorded response is 499.
Figure 4

5. Checking the Nginx configuration of Frontend Service. Because the 50x.html page does not exist, it results in a 404 response. We finally decided to comment out the related configuration items. See Figure 5.

Checking the Nginx configuration of Frontend Service. Because the 50x.html page does not exist, it results in a 404 response. We finally decided to comment out the related configuration items.
Figure 5
Nginx


    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }


6. Analysis conclusion: If the frontend service (frontend-svc-80) is waiting for a response from backend-svc-80, and backend-svc-80 responds too slowly, the client will actively close the connection after receiving a 404 response, causing Backend Service Nginx to log a 499 status code.

7. Detailed flowchart of the current frontend interface request: 1. Client request → 2. Ingress Controller (ingress-nginx-controller) → 3. Frontend Service (frontend-svc-80) → 4. Backend Service (backend-svc-80) → Timeout (504|499) → 5. Frontend Service returns 404 → 6. Ingress Controller returns 404

8. After applying the adjustment from step 5, when the backend interface response times out, it no longer returns a 404, which meets expectations. See Figure 6.

After applying the adjustment from step 5, when the backend interface response times out, it no longer returns a 404, which meets expectations.
Figure 6

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系