nginx 502 error and 504 error

2019-07-16 06:59发布

问题:

My server is node.js and I use nginx as a reverse proxy.

Now I have to do a time-consuming https request, but I always get the 504 Gateway Time-out error. and nginx/error.log says:
"upstream timed out"

So I change nginx conf to:
proxy_read_timeout 600;
proxy_connect_timeout 600;
client_max_body_size 32M;
client_body_buffer_size 512k;
proxy_send_timeout 600;
proxy_buffers 32 4k;
This time there will be no 504 error, but it turn to be 502 error, and nginx/error says:
"upstream prematurely closed connection"
And I find the error will just appear after I start the request for 120s.
My request process will sure take more than 120s, because it will do a time-consuming mysql query.

So I do not know how to get rid of the 502 error.

回答1:

From what you describe, you problem is on the server now. You need to set the timeout both on nginx and t he web server. By default, node has a default timeout of 2 minutes.

Look at the server.setTimeout option: http://nodejs.org/api/http.html#http_server_settimeout_msecs_callback

Hope this helps.



回答2:

You got 504 at first, that is because of configuration error. Then you fixed it.

You got 502 then, which usually means that server is down or read timeout because of limited read time.

Two ways:

1、Adjust your client http read time larger, or the Nginx's permitted maximization read time limitation, usually the former one.

2、Optimize the sql in your request. 2 mins is too long usually may be killed by Mysql.



标签: nginx timeout