Bird
Raised Fist0
Nginxdevops~10 mins

Web server vs application server in Nginx - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Web server vs application server
Client sends HTTP request
Web Server receives request
Static content
Serve file
Application Server processes
Response sent back to Web Server
Web Server sends response to Client
The client sends a request to the web server. The web server serves static files directly or forwards dynamic requests to the application server, which processes them and returns a response.
Execution Sample
Nginx
server {
  listen 80;
  location / {
    root /var/www/html;
    index index.html;
  }
  location /app/ {
    proxy_pass http://localhost:8080;
  }
}
Nginx configuration serving static files from /var/www/html and forwarding /app/ requests to an application server on port 8080.
Process Table
StepRequest URLWeb Server ActionApplication Server ActionResponse Sent
1/index.htmlServe static file /var/www/html/index.htmlN/AStatic HTML content
2/app/dataForward request to app server at localhost:8080Process dynamic request, generate JSONJSON response
3/about.htmlServe static file /var/www/html/about.htmlN/AStatic HTML content
4/app/loginForward request to app server at localhost:8080Process login, generate session tokenSession token response
5/favicon.icoServe static file /var/www/html/favicon.icoN/AIcon file content
💡 All requests handled either by serving static files or forwarding to application server for dynamic content.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Request URLN/A/index.html/app/data/about.html/app/login/favicon.ico
Web Server ActionN/AServe static fileForward to app serverServe static fileForward to app serverServe static file
Application Server ActionN/AN/AProcess dynamic requestN/AProcess loginN/A
Response SentN/AStatic HTMLJSON responseStatic HTMLSession tokenIcon file
Key Moments - 3 Insights
Why does the web server forward some requests to the application server?
Because those requests need dynamic processing which the web server alone cannot do, as shown in execution_table rows 2 and 4.
Can the web server serve dynamic content by itself?
No, the web server serves static files directly but forwards dynamic requests to the application server, as seen in the execution_table where only static URLs are served directly.
What happens if a request is for a static file?
The web server serves the file directly without involving the application server, as shown in execution_table rows 1, 3, and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what action does the web server take for the request '/app/data'?
AServe static file
BForward request to application server
CReturn 404 error
DCache the response
💡 Hint
Check the 'Web Server Action' column for step 2 in the execution_table.
At which step does the application server generate a session token?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Application Server Action' column for step 4 in the execution_table.
If the web server did not forward '/app/login' requests, what would happen?
AStatic login page would be served
BApplication server would still process the request
CRequest would fail or return an error
DRequest would be cached
💡 Hint
Refer to the concept_flow and execution_table showing forwarding is needed for dynamic content.
Concept Snapshot
Web server handles HTTP requests.
Serves static files directly.
Forwards dynamic requests to application server.
Application server processes logic and returns response.
Nginx example: static content served from root, dynamic proxied to app server.
Full Transcript
When a client sends a request, the web server first checks if it is for static content like HTML or images. If yes, it serves the file directly. If the request is for dynamic content, such as data or login, the web server forwards it to the application server. The application server processes the request, runs any needed code, and sends back a response. The web server then sends this response to the client. This separation helps handle static and dynamic content efficiently.

Practice

(1/5)
1. What is the primary role of a web server like nginx?
easy
A. To deliver static files like images and HTML directly to users
B. To execute backend application logic and generate dynamic content
C. To store user data and manage databases
D. To compile source code into executable programs

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    To deliver static files like images and HTML directly to users -> Option A
  4. Quick Check:

    Web server = static file delivery [OK]
Hint: Web servers serve files; app servers run code [OK]
Common Mistakes:
  • Confusing web server with application server
  • Thinking web server runs backend logic
  • Assuming web server manages databases
2. Which nginx configuration snippet correctly forwards requests to an application server on port 3000?
easy
A. location / { root http://localhost:3000; }
B. location / { fastcgi_pass http://localhost:3000; }
C. location / { proxy_pass http://localhost:3000; }
D. location / { redirect http://localhost:3000; }

Solution

  1. Step 1: Identify correct directive for forwarding

    The proxy_pass directive is used in nginx to forward requests to an application server.
  2. Step 2: Check other options for correctness

    root serves static files, fastcgi_pass is for FastCGI servers, and redirect sends HTTP redirects, not proxying.
  3. Final Answer:

    location / { proxy_pass http://localhost:3000; } -> Option C
  4. Quick Check:

    Use proxy_pass to forward requests [OK]
Hint: proxy_pass forwards; root serves static files [OK]
Common Mistakes:
  • Using root instead of proxy_pass for forwarding
  • Confusing fastcgi_pass with proxy_pass
  • Using redirect which changes URL instead of proxying
3. Given this nginx config snippet, what happens when a user requests /app?
location /app {
  proxy_pass http://127.0.0.1:8080;
}
medium
A. nginx serves static files from /app directory
B. nginx forwards the request to the application server at 127.0.0.1:8080
C. nginx returns a 404 error
D. nginx redirects the user to http://127.0.0.1:8080/app

Solution

  1. Step 1: Analyze the proxy_pass directive

    The proxy_pass directive tells nginx to forward matching requests to the specified backend server.
  2. Step 2: Understand request handling

    Requests to /app are sent to the application server at 127.0.0.1:8080, not served as static files or redirected.
  3. Final Answer:

    nginx forwards the request to the application server at 127.0.0.1:8080 -> Option B
  4. Quick Check:

    proxy_pass forwards requests to backend [OK]
Hint: proxy_pass means forward, not serve or redirect [OK]
Common Mistakes:
  • Thinking nginx serves static files here
  • Confusing proxy_pass with redirect
  • Assuming 404 error without backend
4. You configured nginx to forward requests to an application server using:
location /api {
  proxy_pass http://localhost:5000/api;
}
But requests to /api/users fail. What is the likely problem?
medium
A. nginx cannot forward requests to localhost
B. The location block should be location /api/ with trailing slash
C. proxy_pass must use HTTPS instead of HTTP
D. proxy_pass should be http://localhost:5000/ without /api

Solution

  1. 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.
  2. Step 2: Correct proxy_pass to avoid URI duplication

    Removing /api from proxy_pass (using http://localhost:5000/) forwards the full original URI correctly.
  3. Final Answer:

    proxy_pass should be http://localhost:5000/ without /api -> Option D
  4. Quick Check:

    proxy_pass URI affects request path [OK]
Hint: Avoid URI in proxy_pass to prevent path duplication [OK]
Common Mistakes:
  • Including URI in proxy_pass causing double paths
  • Thinking HTTPS is required for localhost
  • Believing nginx can't proxy localhost
5. You want nginx to serve static files from /var/www/html and forward API requests to an application server on port 4000. Which configuration correctly achieves this?
hard
A. location / { root /var/www/html; } location /api/ { proxy_pass http://localhost:4000/; }
B. location / { proxy_pass http://localhost:4000/; } location /api/ { root /var/www/html; }
C. location /api/ { root /var/www/html; } location / { proxy_pass http://localhost:4000/; }
D. location /api/ { proxy_pass http://localhost:4000/api/; } location / { root /var/www/html; }

Solution

  1. Step 1: Assign root for static files

    The root directive in location / serves static files from /var/www/html.
  2. Step 2: Forward API requests correctly

    The location /api/ block uses proxy_pass to forward API calls to the application server on port 4000.
  3. Step 3: Verify order and correctness

    Static files served at root, API forwarded properly. Other options mix these roles incorrectly.
  4. Final Answer:

    location / { root /var/www/html; } location /api/ { proxy_pass http://localhost:4000/; } -> Option A
  5. Quick Check:

    Static files root + API proxy_pass = correct setup [OK]
Hint: Serve static at / root, proxy API at /api/ [OK]
Common Mistakes:
  • Swapping root and proxy_pass locations
  • Adding URI in proxy_pass causing path issues
  • Not using trailing slash in location /api/