0
0
Nginxdevops~30 mins

Nested location blocks in Nginx - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Since this is a configuration file, printing means showing the full text content.