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

Resolving 504 Gateway Time-out When Frontend Reverse Proxies to Backend in Nginx

Resolving 504 Gateway Time-out When Frontend Reverse Proxies to Backend in Nginx

1. Directly request the backend API in Postman, response is 401. As expected. See Figure 1

Directly request the backend API in Postman, response is 401. As expected
Figure 1

2. Directly request the frontend API in Postman, response is 504 Gateway Time-out. Not as expected. See Figure 2

Directly request the frontend API in Postman, response is 504 Gateway Time-out. Not as expected
Figure 2
HTML
<html>
 
<head>
    <title>504 Gateway Time-out</title>
</head>
 
<body>
    <center>
        <h1>504 Gateway Time-out</h1>
    </center>
    <hr>
    <center>nginx</center>
</body>
 
</html>

3. The frontend configuration file in Nginx is as follows

Nginx
server {
	listen 80;
    listen 443 ssl;
    server_name xxx-frontend.com;
    root E:/wwwroot/xxx-frontend/dist/apps/admin;

	access_log  logs/frontend.access.log;
    error_log   logs/frontend.error.log;

    # add_header X-Frame-Options "SAMEORIGIN";
    # add_header X-Content-Type-Options "nosniff";

	# client_max_body_size 1024M;

    ssl_certificate vhosts/xxx-frontend.com.pem;
    ssl_certificate_key vhosts/xxx-frontend.com-key.pem;

    index index.html;

    charset utf-8;

	location /api {
		proxy_pass https://xxx-backend.com/api;
	}

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

4. Check the backend API request logs and confirm that no new request logs are added when requesting the frontend, indicating that the request did not reach the backend. Check the frontend request error logs, details are as follows. See Figure 3

Check the backend API request logs and confirm that no new request logs are added when requesting the frontend, indicating that the request did not reach the backend. Check the frontend request error logs, details are as follows
Figure 3
Plaintext
2024/01/03 11:45:21 [error] 21024#23748: *1121 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: xxx-frontend.com, request: "POST /api/me HTTP/1.1", upstream: "https://120.79.70.168:443/api/me", host: "xxx-frontend.com"

5. Directly POST request https://120.79.70.168:443/api/me and confirm there is no response. Click Disable SSL Verification. See Figure 4

Directly POST request https://120.79.70.168:443/api/me and confirm there is no response. Click Disable SSL Verification
Figure 4

6. The suspected reason is that the proxied URL itself is an intranet penetration URL based on FRP. Decide to directly proxy to the backend URL of the local environment 127.0.0.1, bypassing FRP. Edit the hosts file

Plaintext
127.0.0.1 xxx-frontend.com
127.0.0.1 xxx-backend.com

7. Directly request the frontend API again in Postman, response is 401, consistent with the response from directly requesting the backend API. As expected. See Figure 5

Directly request the frontend API again in Postman, response is 401, consistent with the response from directly requesting the backend API. As expected
Figure 5

8. However, after doing this, Chrome browser prompts that the site is not secure. Finally, I had to revert the changes to the hosts file.

9. Edit the frontend Nginx configuration. The backend Nginx configuration needs to listen on port 8000.

Nginx
	location /api {
		proxy_pass http://localhost:8000/api;
	}

10. Directly request the frontend API again in Postman, response is 401, consistent with the response from directly requesting the backend API. Moreover, the frontend page can also be opened normally in the browser. See Figure 6

Directly request the frontend API again in Postman, response is 401, consistent with the response from directly requesting the backend API. Moreover, the frontend page can also be opened normally in the browser
Figure 6

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

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

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

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

关于我  ·  GitHub  ·  邮件联系