Introduction
A 504 Gateway Timeout error is one of the most common and frustrating problems when running applications behind Nginx, especially on a VPS.
I personally encountered this issue while deploying a production backend behind Nginx as a reverse proxy.
In this article, I’ll explain what causes a 504 Gateway Timeout, how to identify the real problem, and how to fix it properly using real VPS examples — not theory.
What Is a 504 Gateway Timeout?
A 504 Gateway Timeout occurs when:
-
Nginx acts as a reverse proxy
-
Nginx forwards a request to an upstream server (backend)
-
The upstream server does not respond in time
So technically:
Nginx is working, but your backend is slow, unreachable, or misconfigured.
Common Causes of 504 Errors on VPS
From real-world cases, these are the most frequent causes:
1. Backend server is down
-
Node.js / Python / Go app not running
-
Process crashed
-
Wrong port
2. Timeout configuration too low
-
Default Nginx timeout is often too short
-
Backend needs more processing time
3. Wrong reverse proxy configuration
-
Incorrect
proxy_pass -
Missing headers
-
HTTPS upstream without proper config
4. Network or DNS issue
-
Upstream domain not reachable
-
SSL handshake delay
Step 1: Check If Your Backend Is Running
Before touching Nginx, always check your backend first.
Example (Node.js backend on port 3000):
If:
-
Connection refused → backend not running
-
No response → backend frozen
-
Response OK → move to Nginx config
Also check running processes:
Step 3: Fix Nginx Timeout Configuration
This is the most common fix.
Open your Nginx config:
Add or update these values:
Important
-
proxy_read_timeoutis the most critical -
Increase it if your backend does heavy processing
Step 4: Test and Reload Nginx
Always test before reload:
If OK:
Comments
Post a Comment