Why reverse proxying serves backend applications in Nginx - Performance Analysis
We want to understand how the work done by nginx changes when it acts as a reverse proxy for backend servers.
Specifically, how does the number of requests affect the processing time?
Analyze the time complexity of the following nginx reverse proxy configuration.
server {
listen 80;
location / {
proxy_pass http://backend_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
This configuration forwards incoming requests to a backend server while passing client info.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each incoming HTTP request and forwarding it to the backend.
- How many times: Once per request, repeated for every client connection.
Each new request adds one more forwarding operation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 request forwards |
| 100 | 100 request forwards |
| 1000 | 1000 request forwards |
Pattern observation: The work grows directly with the number of requests.
Time Complexity: O(n)
This means the time to handle requests grows linearly as more requests come in.
[X] Wrong: "Reverse proxying handles all requests instantly regardless of load."
[OK] Correct: Each request requires processing and forwarding, so more requests mean more work and time.
Understanding how request load affects reverse proxy performance shows you grasp real-world server behavior and scaling.
What if we added caching to the reverse proxy? How would the time complexity change?
Practice
Solution
Step 1: Understand the role of a reverse proxy
A reverse proxy sits between users and backend servers, forwarding requests and responses.Step 2: Identify the main purpose
It improves security and performance by hiding backend details and managing traffic.Final Answer:
To act as a middleman between users and backend servers -> Option CQuick Check:
Reverse proxy = Middleman [OK]
- Thinking reverse proxy replaces backend apps
- Assuming it stores user data
- Believing it exposes backend directly
Solution
Step 1: Identify nginx directives related to proxying
The directive that forwards requests to backend servers isproxy_pass.Step 2: Differentiate from other directives
server_namesets domain,listensets port,rootsets file path.Final Answer:
proxy_pass -> Option AQuick Check:
Forward requests = proxy_pass [OK]
- Confusing proxy_pass with server_name
- Using listen to forward requests
- Mistaking root for proxying
http://example.com/api/?
location /api/ {
proxy_pass http://backend_server/;
}Solution
Step 1: Understand proxy_pass behavior with trailing slash
When proxy_pass URL ends with a slash, nginx replaces the matching part of the request URI.Step 2: Apply to given config
Request to /api/ is forwarded to backend_server root /, so /api/ is replaced by /.Final Answer:
Requests to /api/ are forwarded to http://backend_server/ -> Option AQuick Check:
proxy_pass with trailing slash replaces matching URI [OK]
- Assuming URI is appended instead of replaced
- Thinking nginx serves files locally
- Expecting 404 without backend
Solution
Step 1: Understand 502 Bad Gateway meaning
502 means nginx cannot connect to the backend server to get a valid response.Step 2: Identify common causes
The backend server being down or unreachable is the most common cause of 502 errors.Final Answer:
Backend server is down or unreachable -> Option DQuick Check:
502 error = backend unreachable [OK]
- Blaming missing semicolon for 502 error
- Confusing port listen issues with backend errors
- Assuming server_name causes 502
Solution
Step 1: Separate static and API requests
Static files should be served from nginx root for /, API requests proxied under /api/.Step 2: Check config snippet correctness
location /api/ { proxy_pass http://backend/; } location / { root /var/www/html; } correctly proxies /api/ to backend and serves / from local files.Final Answer:
location /api/ { proxy_pass http://backend/; } location / { root /var/www/html; } -> Option BQuick Check:
Static on /, API on /api/ with proxy_pass [OK]
- Reversing proxy and root locations
- Combining proxy_pass and root in same block
- Serving API requests from root directory
Ready to prove it?
Pass the test and this pattern moves unknown
