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
Understanding Web Server vs Application Server with Nginx
📖 Scenario: You are setting up a simple web environment to understand how a web server and an application server work together. You will configure Nginx as a web server to serve static files and forward dynamic requests to an application server.
🎯 Goal: Build a basic Nginx configuration that serves static HTML files and proxies requests for dynamic content to an application server running on port 8080.
📋 What You'll Learn
Create a basic Nginx server block to serve static files from /var/www/html
Add a configuration variable for the application server address http://127.0.0.1:8080
Configure Nginx to proxy requests starting with /app/ to the application server
Print the final Nginx configuration to verify the setup
💡 Why This Matters
🌍 Real World
Web servers like Nginx serve static content and forward dynamic requests to application servers, which run the application logic. This separation improves performance and scalability.
💼 Career
Understanding how to configure web servers and application servers is essential for DevOps roles, system administrators, and backend engineers to deploy and manage web applications efficiently.
Progress0 / 4 steps
1
Create basic Nginx server block for static files
Write the Nginx server block configuration to listen on port 80 and serve static files from /var/www/html using the root directive.
Nginx
Hint
Use listen 80; to listen on HTTP port and root /var/www/html; to serve static files.
2
Add application server address variable
Add a variable set $app_server http://127.0.0.1:8080; inside the server block to define the application server address.
Nginx
Hint
Use the set directive inside the server block to create the variable.
3
Configure proxy for application server requests
Add a location /app/ block inside the server block that proxies requests to the application server using proxy_pass $app_server;.
Nginx
Hint
Use location /app/ { proxy_pass $app_server; } to forward requests to the app server.
4
Print the final Nginx configuration
Print the entire Nginx configuration stored in the variable nginx_config to verify the setup.
Nginx
Hint
Use print(nginx_config) to display the configuration.
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
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 A
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
Step 1: Identify correct directive for forwarding
The proxy_pass directive is used in nginx to forward requests to an application server.
Step 2: Check other options for correctness
root serves static files, fastcgi_pass is for FastCGI servers, and redirect sends HTTP redirects, not proxying.
Final Answer:
location / { proxy_pass http://localhost:3000; } -> Option C
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
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 (using http://localhost:5000/) forwards the full original URI correctly.
Final Answer:
proxy_pass should be http://localhost:5000/ without /api -> Option D
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?