Bird
Raised Fist0
Nginxdevops~20 mins

Location 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 Location Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Nginx location block matching
Given the following Nginx configuration snippet, what will be the response for a request to /images/logo.png?
Nginx
server {
    location /images/logo.png {
        return 200 'Logo image';
    }
    location /images/ {
        return 200 'Images folder';
    }
}
A404 Not Found
BLogo image
CImages folder
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Nginx chooses the most specific matching location block.
🧠 Conceptual
intermediate
2:00remaining
Understanding prefix and regex location blocks
Which location block will handle a request to /api/v1/users given this configuration?
Nginx
location /api/ {
    return 200 'API prefix';
}
location ~ ^/api/v1/ {
    return 200 'API v1 regex';
}
ABoth handle it simultaneously
BNeither handles it, 404 returned
CThe regex location ~ ^/api/v1/ handles it
DThe prefix location /api/ handles it
Attempts:
2 left
💡 Hint
Regex locations have higher priority if they match.
Troubleshoot
advanced
2:00remaining
Why does this location block not serve static files?
You have this Nginx config but requests to /static/css/style.css return 404. What is the likely cause?
Nginx
location /static/ {
    root /var/www/html;
}
AThe root directive should be alias instead
BThe root directive is correct; problem is file permissions
CThe root path is incorrect, should be /var/www/html/static
DThe location block should use alias /var/www/html/static/;
Attempts:
2 left
💡 Hint
Understand how root and alias work with location paths.
🔀 Workflow
advanced
2:00remaining
Order of location block evaluation
Arrange the steps Nginx follows to select a location block for a request.
A1,2,3,4
B2,1,3,4
C3,2,1,4
D1,3,2,4
Attempts:
2 left
💡 Hint
Exact matches have highest priority.
Best Practice
expert
2:00remaining
Best practice for combining prefix and regex locations
Which configuration best ensures that requests to /app/ use a prefix location but requests to /app/api/ use a regex location?
Alocation /app/ { ... } location ~ ^/app/api/ { ... }
Blocation ~ ^/app/ { ... } location /app/api/ { ... }
Clocation /app/api/ { ... } location ~ ^/app/ { ... }
Dlocation ~ ^/app/api/ { ... } location /app/ { ... }
Attempts:
2 left
💡 Hint
Regex locations override prefix locations if they match.

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

  1. Step 1: Understand the role of location blocks

    Location blocks in nginx specify rules for handling requests based on URL paths.
  2. 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.
  3. Final Answer:

    To define how nginx handles requests for specific URL paths -> Option C
  4. 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

  1. Step 1: Understand location modifiers

    The = modifier matches the exact URL path.
  2. Step 2: Match syntax to exact URL

    location = /about { } uses = /about which matches exactly '/about'. Others match prefixes or regex.
  3. Final Answer:

    location = /about { } -> Option D
  4. 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

  1. Step 1: Understand root directive with location

    The root directive appends the part of the URI after the location prefix to the root path.
  2. 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.
  3. Final Answer:

    /data/pic.jpg -> Option A
  4. 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

  1. Step 1: Understand alias usage

    When using alias with a location ending with a slash, the alias path must also end with a slash.
  2. Step 2: Check alias path

    Alias path /var/www/static lacks trailing slash, causing incorrect file path resolution.
  3. Final Answer:

    Missing trailing slash in alias path -> Option A
  4. Quick Check:

    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

  1. Step 1: Understand alias vs root behavior

    Alias replaces the location prefix with the alias path exactly, avoiding duplication.
  2. Step 2: Check trailing slashes for alias

    Alias path must end with a slash to match location ending with slash, ensuring correct path mapping.
  3. 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.
  4. Final Answer:

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

    Alias with trailing slash avoids duplication = A [OK]
Hint: Use alias with trailing slash to avoid path duplication [OK]
Common Mistakes:
  • Using root causing duplicated /static/ in path
  • Omitting trailing slash on alias path
  • Confusing alias and root usage