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
Configuring Nginx Location Blocks
📖 Scenario: You are setting up a simple web server using Nginx. You want to serve different content based on the URL path requested by users.For example, requests to /images should serve image files from a specific folder, and requests to /api should be forwarded to a backend server.
🎯 Goal: Learn how to create and configure location blocks in an Nginx configuration file to route requests based on URL paths.
📋 What You'll Learn
Create a basic Nginx server block
Add a location block for /images to serve static files
Add a location block for /api to proxy requests to a backend server
Print the final Nginx configuration
💡 Why This Matters
🌍 Real World
Nginx is widely used to serve websites and route requests to different services. Location blocks help control how different URL paths are handled.
💼 Career
Understanding Nginx configuration and location blocks is essential for DevOps roles managing web servers and application deployments.
Progress0 / 4 steps
1
Create the basic Nginx server block
Create a variable called nginx_config and assign it a string containing a basic Nginx server block listening on port 80 with the server name example.com.
Nginx
Hint
Start with server { and include listen 80; and server_name example.com; inside.
2
Add a location block for /images
Add a location block for /images inside the nginx_config string that serves static files from /var/www/images. Append this block inside the existing server block.
Nginx
Hint
Use location /images { and inside set root /var/www; to serve files from /var/www/images.
3
Add a location block for /api to proxy requests
Add a location block for /api inside the nginx_config string that proxies requests to http://localhost:3000. Append this block inside the existing server block after the /images location.
Nginx
Hint
Use location /api { and inside set proxy_pass http://localhost:3000; to forward requests.
4
Print the final Nginx configuration
Write a print statement to display the full nginx_config string.
Nginx
Hint
Use print(nginx_config) to show the full configuration.
Practice
(1/5)
1. What is the main purpose of a location block in an nginx configuration?
easy
A. To specify the server's hostname
B. To set the server's IP address
C. To define how nginx handles requests for specific URL paths
D. To configure the database connection
Solution
Step 1: Understand the role of location blocks
Location blocks in nginx specify rules for handling requests based on URL paths.
Step 2: Compare options with location block purpose
Only To define how nginx handles requests for specific URL paths correctly describes this purpose; others relate to different server settings.
Final Answer:
To define how nginx handles requests for specific URL paths -> Option C
Quick Check:
Location blocks control URL handling = D [OK]
Hint: Location blocks match URLs to control request handling [OK]
Common Mistakes:
Confusing location blocks with server settings
Thinking location blocks set server IP or hostname
Mixing location blocks with database configs
2. Which of the following is the correct syntax to define a location block that matches the exact URL /about?
easy
A. location /about { }
B. location ~ /about { }
C. location ^~ /about { }
D. location = /about { }
Solution
Step 1: Understand location modifiers
The = modifier matches the exact URL path.
Step 2: Match syntax to exact URL
location = /about { } uses = /about which matches exactly '/about'. Others match prefixes or regex.
Final Answer:
location = /about { } -> Option D
Quick Check:
Exact match uses '=' modifier = C [OK]
Hint: Use '=' for exact URL match in location block [OK]
Common Mistakes:
Using no modifier for exact match
Confusing regex (~) with exact match
Using ^~ which is prefix, not exact
3. Given this nginx config snippet:
location /images/ {
root /data;
}
What is the full file path nginx will serve for a request to /images/pic.jpg?
medium
A. /data/pic.jpg
B. /data/images/pic.jpg
C. /images/pic.jpg
D. /data/images/
Solution
Step 1: Understand root directive with location
The root directive appends the part of the URI after the location prefix to the root path.
Step 2: Combine root and URI
Location prefix is /images/, request URI is /images/pic.jpg, so the part after prefix is pic.jpg. Root is /data, so full path is /data/pic.jpg.
Final Answer:
/data/pic.jpg -> Option A
Quick Check:
root + URI after location prefix = /data/pic.jpg [OK]
Hint: root + URI after location prefix = file path served [OK]
Common Mistakes:
Assuming root combines with full URI
Using full URI instead of URI after location prefix
Confusing alias with root behavior
4. Identify the error in this nginx location block:
location /static/ {
alias /var/www/static;
}
medium
A. Missing trailing slash in alias path
B. alias should be root here
C. location path should not end with slash
D. No error, configuration is correct
Solution
Step 1: Understand alias usage
When using alias with a location ending with a slash, the alias path must also end with a slash.
Alias path must end with '/' if location ends with '/' = B [OK]
Hint: Alias path needs trailing slash if location ends with slash [OK]
Common Mistakes:
Using root instead of alias incorrectly
Omitting trailing slash on alias path
Thinking location path cannot end with slash
5. You want nginx to serve static files from /var/www/app/static when users request URLs starting with /static/, but you want to avoid duplicating the /static/ part in the file path. Which location block correctly achieves this?
hard
A. location /static/ { root /var/www/app/static; }
B. location /static/ { alias /var/www/app/static/; }
C. location /static/ { alias /var/www/app/static; }
D. location /static/ { root /var/www/app; }
Solution
Step 1: Understand alias vs root behavior
Alias replaces the location prefix with the alias path exactly, avoiding duplication.
Step 2: Check trailing slashes for alias
Alias path must end with a slash to match location ending with slash, ensuring correct path mapping.
Step 3: Evaluate options
location /static/ { alias /var/www/app/static/; } uses alias with trailing slash, correctly mapping /static/file to /var/www/app/static/file. Others either duplicate path or miss slash.
Final Answer:
location /static/ { alias /var/www/app/static/; } -> Option B
Quick Check:
Alias with trailing slash avoids duplication = A [OK]
Hint: Use alias with trailing slash to avoid path duplication [OK]