0
0
Nginxdevops~20 mins

Named locations (@) in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Named Locations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output status code when accessing /test?
Given the following nginx configuration, what HTTP status code will the client receive when requesting /test?
Nginx
server {
    listen 80;
    location /test {
        return 302 @redirect;
    }
    location @redirect {
        return 404;
    }
}
A404 Not Found
B200 OK
C302 Found
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Look at what the named location @redirect returns.
Configuration
intermediate
2:00remaining
Which configuration correctly uses a named location to serve a maintenance page?
Select the nginx configuration snippet that correctly uses a named location @maintenance to serve a static maintenance page when /maintenance is requested.
A
location /maintenance {
    proxy_pass @maintenance;
}
location @maintenance {
    root /var/www/html;
    try_files /maintenance.html =404;
}
B
location /maintenance {
    return 302 @maintenance;
}
location @maintenance {
    root /var/www/html;
    try_files /maintenance.html =404;
}
C
location /maintenance {
    rewrite ^ /maintenance.html break;
}
location @maintenance {
    root /var/www/html;
    try_files /maintenance.html =404;
}
D
location /maintenance {
    return 301 /maintenance.html;
}
location @maintenance {
    root /var/www/html;
    try_files /maintenance.html =404;
}
Attempts:
2 left
💡 Hint
Named locations are used with return or error_page directives, not proxy_pass.
Troubleshoot
advanced
2:00remaining
Why does the named location @error not get triggered?
Given this nginx config, why does the named location @error never get used when a 500 error occurs?
server {
    listen 80;
    location / {
        proxy_pass http://backend;
        error_page 500 @error;
    }
    location @error {
        return 503 'Service temporarily unavailable';
    }
}
AThe error_page directive must be outside the location block to work with named locations.
BNamed locations cannot be used with error_page directives.
CThe backend server does not return a 500 error, so @error is never triggered.
DThe return directive inside @error is invalid and causes the location to be ignored.
Attempts:
2 left
💡 Hint
Check the backend server response and error_page usage.
🔀 Workflow
advanced
2:00remaining
What is the sequence of location processing when using named locations?
Consider this nginx configuration:
location /start {
    return 302 @next;
}
location @next {
    proxy_pass http://backend;
}
What is the correct sequence of processing when a client requests /start?
AClient requests /start → nginx returns 302 redirect to @next → client requests @next → nginx proxies to backend
BClient requests /start → nginx internally redirects to @next → nginx proxies to backend without client redirect
CClient requests /start → nginx returns 200 OK with content from @next location
DClient requests /start → nginx returns 404 because @next is a named location
Attempts:
2 left
💡 Hint
Named locations starting with @ are internal and cannot be requested directly by clients.
Best Practice
expert
2:30remaining
Which is the best way to use named locations for internal rewrites without client redirects?
You want to internally rewrite requests to /oldpath to a named location @newpath without telling the client to redirect. Which configuration snippet achieves this correctly?
A
location /oldpath {
    proxy_pass @newpath;
}
location @newpath {
    proxy_pass http://backend;
}
B
location /oldpath {
    return 302 @newpath;
}
location @newpath {
    proxy_pass http://backend;
}
C
location /oldpath {
    rewrite ^ /newpath break;
}
location @newpath {
    proxy_pass http://backend;
}
D
location /oldpath {
    error_page 418 = @newpath;
    return 418;
}
location @newpath {
    proxy_pass http://backend;
}
Attempts:
2 left
💡 Hint
Use error_page with a custom code to trigger internal named location rewrites.