Bird
Raised Fist0
Nginxdevops~20 mins

Directives and blocks in Nginx - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Nginx Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of nginx configuration test with nested blocks
Given the following nginx configuration snippet, what will be the output of nginx -t command?
Nginx
http {
    server {
        listen 80;
        location / {
            root /var/www/html;
        }
    }
    location /images/ {
        root /var/www/images;
    }
}
A
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
B
nginx: [warn] duplicate location "/images/" in /etc/nginx/nginx.conf:8
nginx: configuration file /etc/nginx/nginx.conf test failed
C
nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:6
nginx: configuration file /etc/nginx/nginx.conf test failed
D
nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:8
nginx: configuration file /etc/nginx/nginx.conf test failed
Attempts:
2 left
💡 Hint
Check where location blocks are allowed inside nginx configuration.
🧠 Conceptual
intermediate
1:30remaining
Understanding directive context in nginx
Which of the following directives can only be used inside a server block and not directly inside the http block?
Ainclude
Blisten
Clog_format
Dworker_processes
Attempts:
2 left
💡 Hint
Think about where nginx listens for connections.
Troubleshoot
advanced
2:30remaining
Troubleshooting nginx block nesting error
You added this snippet to your nginx config but get an error on reload. What is the cause?
Nginx
server {
    listen 443 ssl;
    ssl_certificate /etc/ssl/cert.pem;
    ssl_certificate_key /etc/ssl/key.pem;
    server_name example.com;
    http {
        location / {
            proxy_pass http://localhost:3000;
        }
    }
}
AThe <code>http</code> block cannot be nested inside a <code>server</code> block.
BThe <code>ssl_certificate</code> directive is misplaced and should be inside <code>location</code>.
CThe <code>proxy_pass</code> directive must be outside any block.
DThe <code>server_name</code> directive is missing a semicolon.
Attempts:
2 left
💡 Hint
Check nginx block hierarchy rules.
🔀 Workflow
advanced
2:00remaining
Order of nginx configuration blocks for a basic web server
What is the correct order of nginx configuration blocks from top-level to nested for a basic HTTP server?
A2,3,1,4
B3,2,1,4
C2,1,3,4
D1,3,2,4
Attempts:
2 left
💡 Hint
Think about the main blocks and their nesting levels.
Best Practice
expert
3:00remaining
Best practice for organizing nginx directives and blocks
Which practice is best for organizing nginx configuration files to improve maintainability and clarity?
APlace all directives inside the main <code>nginx.conf</code> file without includes.
BAvoid using <code>include</code> directives to prevent confusion.
CUse multiple <code>server</code> blocks inside <code>http</code> and split configuration into separate files included with <code>include</code> directive.
DPut <code>location</code> blocks directly inside the <code>http</code> block for simplicity.
Attempts:
2 left
💡 Hint
Think about modularity and ease of updates.

Practice

(1/5)
1. What is the main difference between a directive and a block in an nginx configuration?
easy
A. A directive groups multiple blocks; a block is a single instruction ending with a semicolon.
B. A directive is a single instruction ending with a semicolon; a block groups multiple directives inside curly braces.
C. A directive is used only for server settings; a block is used only for location settings.
D. A directive must always contain a block inside it.

Solution

  1. Step 1: Understand directive syntax

    A directive is a single instruction that ends with a semicolon in nginx configuration.
  2. Step 2: Understand block syntax

    A block groups multiple directives inside curly braces to organize related settings.
  3. Final Answer:

    A directive is a single instruction ending with a semicolon; a block groups multiple directives inside curly braces. -> Option B
  4. Quick Check:

    Directive = single instruction; Block = group of directives [OK]
Hint: Directives end with ; blocks use { } to group [OK]
Common Mistakes:
  • Confusing directives with blocks
  • Thinking blocks end with semicolon
  • Assuming directives can contain blocks
2. Which of the following is the correct syntax for an nginx directive?
easy
A. listen 80;
B. server { listen 80 }
C. location / { listen 80 }
D. listen 80

Solution

  1. Step 1: Identify directive syntax

    A directive must end with a semicolon and is a single instruction.
  2. Step 2: Check each option

    listen 80; ends with a semicolon and is a single instruction: listen 80;.
  3. Final Answer:

    listen 80; -> Option A
  4. Quick Check:

    Directive ends with ; [OK]
Hint: Directives always end with a semicolon ; [OK]
Common Mistakes:
  • Omitting semicolon at end
  • Using curly braces for directives
  • Mixing block syntax with directive
3. Given this nginx configuration snippet, what will happen when a request is made to /images?
location /images/ {
    root /data;
    autoindex on;
}
medium
A. Nginx will return a 404 error because root is incorrectly used.
B. Nginx will serve files from /images/ directory on the server root.
C. Nginx will redirect requests to /data/images/ automatically.
D. Nginx will serve files from /data/images/ and show a directory listing if no index file exists.

Solution

  1. Step 1: Understand the location block

    The location block matches requests starting with /images/.
  2. Step 2: Interpret the root directive

    Root sets the base directory to /data, so files are served from /data/images/.
  3. Step 3: Effect of autoindex on

    If no index file exists, nginx shows a directory listing.
  4. Final Answer:

    Nginx will serve files from /data/images/ and show a directory listing if no index file exists. -> Option D
  5. Quick Check:

    location + root + autoindex = serve files with listing [OK]
Hint: root sets base path; autoindex shows directory listing [OK]
Common Mistakes:
  • Confusing root with alias
  • Assuming redirect happens automatically
  • Ignoring autoindex effect
4. Identify the error in this nginx configuration snippet:
server {
    listen 80
    server_name example.com;
}
medium
A. listen directive should be inside location block.
B. server_name directive cannot be inside server block.
C. Missing semicolon after listen 80 directive.
D. Curly braces are missing around listen directive.

Solution

  1. Step 1: Check syntax of directives

    Each directive must end with a semicolon in nginx configuration.
  2. Step 2: Locate missing semicolon

    The listen 80 directive is missing a semicolon at the end.
  3. Final Answer:

    Missing semicolon after listen 80 directive. -> Option C
  4. Quick Check:

    Every directive ends with ; [OK]
Hint: Check every directive ends with ; [OK]
Common Mistakes:
  • Forgetting semicolon at directive end
  • Misplacing directives outside server block
  • Adding unnecessary braces
5. You want to configure nginx to serve static files from /var/www/html for all requests under /static/. Which configuration block correctly achieves this?
hard
A. location /static/ { alias /var/www/html/; }
B. location /static { alias /var/www/html; }
C. location /static/ { root /var/www/html; }
D. location /static/ { root /var/www/html/; }

Solution

  1. Step 1: Understand root vs alias

    Root appends the request URI to the root path; alias replaces the location prefix with the alias path.
  2. Step 2: Match location and alias usage

    For prefix locations ending with /, alias must end with / to correctly map paths.
  3. Step 3: Evaluate options

    location /static/ { alias /var/www/html/; } uses location /static/ { alias /var/www/html/; } which correctly serves files under /static/ from /var/www/html.
  4. Final Answer:

    location /static/ { alias /var/www/html/; } -> Option A
  5. Quick Check:

    Use alias with trailing slash for prefix location [OK]
Hint: Use alias with trailing slash for prefix locations [OK]
Common Mistakes:
  • Using root instead of alias for prefix paths
  • Missing trailing slash on alias path
  • Mismatching location and alias slashes