Web server vs application server in Nginx - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the work done by a web server like nginx grows as it handles more requests.
How does the server's processing time change when more users connect?
Analyze the time complexity of the following nginx configuration snippet.
server {
listen 80;
location / {
proxy_pass http://app_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
This snippet shows nginx acting as a web server forwarding requests to an application server.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each incoming HTTP request and forwarding it.
- How many times: Once per request, repeated for every user connection.
As the number of requests increases, nginx processes each one individually.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 request handlings |
| 100 | 100 request handlings |
| 1000 | 1000 request handlings |
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: "The web server handles all requests instantly no matter how many users connect."
[OK] Correct: Each request takes some time to process, so more requests mean more total work.
Understanding how request handling scales helps you explain server performance clearly and confidently.
"What if nginx cached responses instead of forwarding every request? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of a web server
A web server mainly serves static content such as images, HTML, CSS, and JavaScript files directly to users.Step 2: Differentiate from application server
An application server runs backend code to create dynamic content, which is different from a web server's role.Final Answer:
To deliver static files like images and HTML directly to users -> Option AQuick Check:
Web server = static file delivery [OK]
- Confusing web server with application server
- Thinking web server runs backend logic
- Assuming web server manages databases
Solution
Step 1: Identify correct directive for forwarding
Theproxy_passdirective is used in nginx to forward requests to an application server.Step 2: Check other options for correctness
rootserves static files,fastcgi_passis for FastCGI servers, andredirectsends HTTP redirects, not proxying.Final Answer:
location / { proxy_pass http://localhost:3000; } -> Option CQuick Check:
Use proxy_pass to forward requests [OK]
- Using root instead of proxy_pass for forwarding
- Confusing fastcgi_pass with proxy_pass
- Using redirect which changes URL instead of proxying
/app?
location /app {
proxy_pass http://127.0.0.1:8080;
}Solution
Step 1: Analyze the proxy_pass directive
Theproxy_passdirective tells nginx to forward matching requests to the specified backend server.Step 2: Understand request handling
Requests to/appare sent to the application server at 127.0.0.1:8080, not served as static files or redirected.Final Answer:
nginx forwards the request to the application server at 127.0.0.1:8080 -> Option BQuick Check:
proxy_pass forwards requests to backend [OK]
- Thinking nginx serves static files here
- Confusing proxy_pass with redirect
- Assuming 404 error without backend
location /api {
proxy_pass http://localhost:5000/api;
}
But requests to /api/users fail. What is the likely problem?Solution
Step 1: Understand proxy_pass URI behavior
When proxy_pass includes a URI (like /api), nginx replaces the matching part of the request URI with that URI, causing duplication.Step 2: Correct proxy_pass to avoid URI duplication
Removing /api from proxy_pass (usinghttp://localhost:5000/) forwards the full original URI correctly.Final Answer:
proxy_pass should be http://localhost:5000/ without /api -> Option DQuick Check:
proxy_pass URI affects request path [OK]
- Including URI in proxy_pass causing double paths
- Thinking HTTPS is required for localhost
- Believing nginx can't proxy localhost
/var/www/html and forward API requests to an application server on port 4000. Which configuration correctly achieves this?Solution
Step 1: Assign root for static files
The root directive inlocation /serves static files from/var/www/html.Step 2: Forward API requests correctly
Thelocation /api/block usesproxy_passto forward API calls to the application server on port 4000.Step 3: Verify order and correctness
Static files served at root, API forwarded properly. Other options mix these roles incorrectly.Final Answer:
location / { root /var/www/html; } location /api/ { proxy_pass http://localhost:4000/; } -> Option AQuick Check:
Static files root + API proxy_pass = correct setup [OK]
- Swapping root and proxy_pass locations
- Adding URI in proxy_pass causing path issues
- Not using trailing slash in location /api/
