When a backend host is off, a TCP connect can hang until it times out. nginx waits 60 seconds for that by default, and the visitor stares at a spinner the whole time before getting a 504.
location / {
proxy_pass http://app;
proxy_connect_timeout 3s;
proxy_read_timeout 60s;
}
proxy_connect_timeout covers only the TCP connection to the backend. A server on the same network answers that in milliseconds, so a few seconds is plenty. With an upstream group, nginx gives up on the dead server after 3 seconds and moves on to the next one, so the page still loads, a bit slow, instead of failing after a minute.
It does not limit how long the backend may take to answer. That is proxy_read_timeout, and you want that one longer for slow reports or uploads. Mixing up the two is the usual mistake.
Don’t go too low either. Over a VPN or to a host on another continent, a connect can take a few hundred milliseconds, and a value under a second then causes random 502s. I’d stay at two seconds or more. The docs also note that the value usually cannot go above 75 seconds, so raising it does not help with a backend that never answers. When the connect fails because nothing listens on the port, nginx gets a refusal right away and the timeout plays no part.
Further reading: proxy_connect_timeout
Going deeper: Guide: fix a 502 Bad Gateway in Nginx Proxy Manager, with a checklist that finds the cause