502 Bad Gateway troubleshooting in Nginx - Time & Space Complexity
When troubleshooting a 502 Bad Gateway error in nginx, it's important to understand how the server handles requests and communicates with backend services.
We want to know how the time taken to process requests grows as the number of incoming requests increases.
Analyze the time complexity of this nginx proxy configuration snippet.
server {
listen 80;
location / {
proxy_pass http://backend_servers;
proxy_connect_timeout 5s;
proxy_read_timeout 10s;
}
}
This snippet forwards client requests to backend servers and waits for their response with set timeouts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each incoming client request triggers a proxy connection to a backend server.
- How many times: This happens once per request, but many requests can come in concurrently or sequentially.
As the number of requests (n) increases, nginx opens more proxy connections to backend servers.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 proxy connections |
| 100 | 100 proxy connections |
| 1000 | 1000 proxy connections |
Pattern observation: The number of proxy connections grows linearly with the number of requests.
Time Complexity: O(n)
This means the work nginx does grows directly in proportion to the number of incoming requests.
[X] Wrong: "The 502 error means nginx is stuck in a loop causing exponential delays."
[OK] Correct: The 502 error usually means a backend server is unreachable or slow, not that nginx is looping. Each request is handled independently, so delays grow linearly, not exponentially.
Understanding how nginx handles many requests and proxies them helps you explain real-world server behavior clearly and calmly.
"What if we added caching in nginx to reduce backend requests? How would the time complexity change?"
Practice
502 Bad Gateway error usually indicate in nginx?Solution
Step 1: Understand the meaning of 502 Bad Gateway
A 502 error means the gateway (nginx) got an invalid response from the backend server it tries to connect to.Step 2: Identify the cause related to backend communication
This error is not about nginx config syntax or server load, but about backend response failure.Final Answer:
nginx cannot get a valid response from the backend server -> Option AQuick Check:
502 Bad Gateway = backend response failure [OK]
- Confusing 502 with nginx config syntax errors
- Thinking 502 means nginx is overloaded
- Assuming 502 is due to disk space issues
Solution
Step 1: Identify the directive that defines backend server address
Theproxy_passdirective tells nginx where to send requests, crucial for proxying.Step 2: Understand other directives' roles
listensets port,server_namesets hostname,rootsets static file location; none define backend proxy.Final Answer:
proxy_pass http://backend_server; -> Option DQuick Check:
proxy_pass sets backend URL [OK]
- Using listen instead of proxy_pass for backend
- Confusing server_name with backend address
- Setting root instead of proxy_pass for proxying
connect() failed (111: Connection refused) while connecting to upstreamWhat is the most likely cause of the 502 error?
Solution
Step 1: Interpret the error message
"Connection refused" means nginx tried to connect but backend server refused, likely because it is down or not listening.Step 2: Rule out other causes
Syntax errors cause config reload failures, permissions affect file access, DNS failure shows different errors.Final Answer:
Backend server is not running or not accepting connections -> Option CQuick Check:
Connection refused = backend down [OK]
- Assuming syntax error from connection refused
- Blaming nginx file permissions
- Thinking DNS failure causes connection refused
upstream timed out. Which fix is most appropriate?Solution
Step 1: Understand the timeout error
"upstream timed out" means nginx waited too long for backend response.Step 2: Adjust nginx timeout settings
Increasingproxy_read_timeoutlets nginx wait longer before giving 502 error.Final Answer:
Increase proxy_read_timeout in nginx config -> Option BQuick Check:
Timeout error fixed by increasing proxy_read_timeout [OK]
- Restarting nginx without config change
- Changing server_name unrelated to timeout
- Clearing browser cache won't fix backend timeout
Solution
Step 1: Apply nginx configuration and backend fixes
After fixing backend, reload or restart nginx to apply any config changes and reset connections.Step 2: Verify fix by checking logs
Check nginx error logs to confirm 502 errors no longer appear, ensuring the problem is solved.Final Answer:
Reload or restart nginx and check error logs for 502 errors -> Option AQuick Check:
Reload nginx and check logs to confirm fix [OK]
- Only clearing browser cache without server reload
- Changing unrelated config like server_name
- Deleting logs instead of reading them
Ready to prove it?
Pass the test and this pattern moves unknown
