proxy_pass directive in Nginx - Time & Space Complexity
When using the proxy_pass directive in nginx, it is important to understand how the processing time changes as the number of incoming requests grows.
We want to know how the server handles more requests and how that affects response time.
Analyze the time complexity of the following nginx configuration snippet.
server {
listen 80;
location /api/ {
proxy_pass http://backend_server;
}
}
This snippet forwards all requests starting with /api/ to a backend server using proxy_pass.
Look at what repeats when nginx handles requests with proxy_pass.
- Primary operation: Forwarding each incoming request to the backend server.
- How many times: Once per request received by nginx.
As the number of requests increases, nginx forwards each one individually.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 forwards to backend |
| 100 | 100 forwards to backend |
| 1000 | 1000 forwards to backend |
Pattern observation: The number of forwarding operations 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: "The proxy_pass directive processes all requests at once, so time stays the same no matter how many requests arrive."
[OK] Correct: Each request is handled separately and forwarded individually, so more requests mean more work and more time.
Understanding how nginx handles requests with proxy_pass helps you explain server behavior clearly and shows you grasp real-world request flow.
"What if we added caching before proxy_pass? How would the time complexity change?"
Practice
proxy_pass directive in an Nginx configuration?Solution
Step 1: Understand the role of
Theproxy_passproxy_passdirective is used to send client requests received by Nginx to another server, often called a backend or upstream server.Step 2: Compare with other options
Serving static files is done byrootoralias, blocking IPs usesdeny, and URL rewriting usesrewrite. These are different fromproxy_pass.Final Answer:
To forward client requests to another server -> Option CQuick Check:
proxy_passforwards requests [OK]
- Confusing proxy_pass with static file serving
- Thinking proxy_pass blocks requests
- Mixing proxy_pass with URL rewriting
/app/ to http://backend.local/ using proxy_pass inside a location /app/ block?Solution
Step 1: Understand proxy_pass URL format
The URL inproxy_passmust include the scheme (http://) and a trailing slash if you want to replace the matching part of the request URI.Step 2: Analyze options
proxy_pass http://backend.local/; useshttp://backend.local/with trailing slash, which correctly forwards requests replacing/app/with/. proxy_pass http://backend.local/app/; appends/app/again, causing duplication. proxy_pass backend.local; lacks scheme, invalid syntax. proxy_pass http://backend.local misses trailing slash, causing different URI handling.Final Answer:
proxy_pass http://backend.local/; -> Option AQuick Check:
Correct proxy_pass URL includes scheme and trailing slash [OK]
- Omitting http:// scheme
- Missing trailing slash causing URI errors
- Appending location path twice
location /api/ {
proxy_pass http://backend.local/;
}If a client requests
/api/users, what will be the proxied request URI sent to http://backend.local?Solution
Step 1: Understand URI replacement with trailing slash
Whenproxy_passURL ends with a slash, Nginx replaces the matching part of the request URI (/api/) with the path inproxy_pass(which is/here).Step 2: Apply to example
Request URI/api/usershas/api/replaced by/, resulting in/userssent to backend.Final Answer:
/users -> Option BQuick Check:
Trailing slash in proxy_pass replaces matching location path [OK]
- Assuming original URI is sent unchanged
- Confusing with proxy_pass without trailing slash
- Thinking /api/ prefix remains in proxied URI
location /service/ {
proxy_pass http://backend.local/service/;
}Clients report 404 errors from backend for requests like
/service/data. What is the likely cause?Solution
Step 1: Analyze proxy_pass with matching location
Whenproxy_passURL includes the same path as location, Nginx appends the full request URI after it, causing duplication.Step 2: Understand effect on backend URI
Request/service/databecomes/service/service/dataat backend, likely causing 404 errors.Final Answer:
proxy_pass URL duplicates the /service/ path causing wrong backend URI -> Option AQuick Check:
Duplicated path in proxy_pass causes 404 [OK]
- Ignoring duplicated path in proxy_pass
- Assuming backend down without checking URI
- Confusing missing slash with duplicated path
/app/ to http://backend.local/ but keep the original /app/ prefix in the proxied URI. Which proxy_pass configuration achieves this?Solution
Step 1: Understand proxy_pass without trailing slash
Ifproxy_passURL has no trailing slash, Nginx appends the full original request URI after the host.Step 2: Apply to example
Usingproxy_pass http://backend.local;insidelocation /app/sends requests like/app/dataashttp://backend.local/app/data, preserving the prefix.Final Answer:
proxy_pass http://backend.local; -> Option DQuick Check:
No trailing slash keeps original URI prefix [OK]
- Adding trailing slash removes original prefix
- Appending /app/ twice causing duplication
- Missing scheme in proxy_pass URL
Ready to prove it?
Pass the test and this pattern moves unknown
