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
Nested location blocks
📖 Scenario: You are setting up a simple web server using nginx. You want to serve different content based on the URL path. For example, the root path should show a welcome page, and a subpath should serve a special section with its own rules.
🎯 Goal: Build an nginx configuration with nested location blocks. The main location / serves the homepage, and a nested location /admin/ serves admin pages with a different root directory.
📋 What You'll Learn
Create a server block listening on port 80
Add a location / block with root /var/www/html
Add a nested location /admin/ block inside location / with root /var/www/admin
Print the final configuration to verify the nested blocks
💡 Why This Matters
🌍 Real World
Web servers often need to serve different content or apply different rules based on URL paths. Nested location blocks help organize these rules clearly.
💼 Career
Understanding <code>nginx</code> configuration and nested location blocks is essential for DevOps roles managing web servers and deploying applications.
Progress0 / 4 steps
1
Create the basic server block
Create an nginx server block that listens on port 80. Inside it, add a location / block with the root directory set to /var/www/html.
Nginx
Hint
Remember to open and close the server and location blocks with curly braces.
2
Add a nested location block for admin
Inside the existing location / block, add a nested location /admin/ block. Set its root directory to /var/www/admin.
Nginx
Hint
Place the location /admin/ block inside the location / block.
3
Add an index directive to the root location
Inside the location / block, add an index directive with the value index.html to serve the homepage file.
Nginx
Hint
The index directive tells nginx which file to serve by default in a directory.
4
Print the final nginx configuration
Print the entire nginx configuration to verify the nested location blocks and directives.
Nginx
Hint
Since this is a configuration file, printing means showing the full text content.
Practice
(1/5)
1. What is the main purpose of using nested location blocks in an nginx configuration?
easy
A. To automatically update nginx software
B. To increase server hardware performance
C. To organize URL handling inside parent paths for clearer rules
D. To disable logging for specific URLs
Solution
Step 1: Understand the role of location blocks
Location blocks define how nginx handles requests for specific URL paths.
Step 2: Recognize the benefit of nesting
Nested location blocks allow specific rules for sub-paths without repeating the parent path, making configuration clearer.
Final Answer:
To organize URL handling inside parent paths for clearer rules -> Option C
Quick Check:
Nested location blocks = organize URL handling [OK]
Hint: Nested blocks group URL rules under parent paths [OK]
Common Mistakes:
Thinking nested blocks improve hardware speed
Confusing nested blocks with software updates
Assuming nested blocks disable logging
2. Which of the following is the correct syntax to define a nested location block inside location /app/ in nginx?
A. Nested location blocks cannot be used inside another location
B. No error; configuration is valid
C. proxy_pass URLs must include trailing slash
D. Nested location paths should not start with a slash
Solution
Step 1: Recall nested location path syntax
Nested location paths inside a parent location should be relative and not start with a slash.
Step 2: Check given nested paths
Nested locations use /v1/ and /v2/ starting with slash, which is incorrect.
Final Answer:
Nested location paths should not start with a slash -> Option D
Quick Check:
Nested paths omit leading slash [OK]
Hint: Nested location paths omit leading slash [OK]
Common Mistakes:
Thinking nested locations are disallowed
Believing proxy_pass must have trailing slash
Assuming config is valid as is
5. You want to serve static files from /var/www/app for /app/ URLs, but proxy API requests under /app/api/ to http://api_backend. Which nested location block setup is correct?