0
0
Nginxdevops~20 mins

Map directive for variable mapping in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Map Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of Nginx map directive with simple variable mapping
Given the following Nginx configuration snippet, what will be the value of the variable $color when the request URI is /blue?
Nginx
map $uri $color {
    default red;
    /blue blue;
    /green green;
}
Aempty string
Bred
Cgreen
Dblue
Attempts:
2 left
💡 Hint
The map directive assigns values based on matching keys; if no match, it uses default.
🧠 Conceptual
intermediate
1:30remaining
Purpose of the Nginx map directive
What is the primary purpose of the Nginx map directive?
ATo set up SSL certificates for HTTPS
BTo rewrite URLs before processing requests
CTo create a new variable by mapping one variable's value to another based on conditions
DTo define server blocks for different domains
Attempts:
2 left
💡 Hint
Think about how you can assign values dynamically based on another variable's value.
Configuration
advanced
2:30remaining
Correct Nginx map syntax for IP-based access control
Which of the following Nginx map configurations correctly sets $allowed to 1 for IP 192.168.1.100 and 0 for all others?
A
map $remote_addr $allowed {
    192.168.1.100 1;
    default 0;
}
B
map $remote_addr $allowed {
    default 0;
    192.168.1.100 1;
}
C
map $remote_addr $allowed {
    192.168.1.100:1;
    default:0;
}
D
map $remote_addr $allowed {
    192.168.1.100 => 1;
    default => 0;
}
Attempts:
2 left
💡 Hint
Check the syntax for key-value pairs inside the map block.
Troubleshoot
advanced
3:00remaining
Troubleshooting unexpected variable value in Nginx map
You have this map directive:
map $http_user_agent $is_mobile {
    default 0;
    ~*Mobile 1;
}
But $is_mobile is always 0 even when accessing from a mobile browser. What is the most likely cause?
AThe variable <code>$http_user_agent</code> is empty or not passed correctly
BThe map directive does not support regular expressions
CThe <code>~*</code> regex pattern is case-sensitive and should be <code>~</code>
DThe map directive must be placed inside the server block, not http block
Attempts:
2 left
💡 Hint
Consider if the variable used in the map has the expected value during request processing.
🔀 Workflow
expert
3:00remaining
Order of evaluation in Nginx map directive with overlapping keys
Consider this map directive:
map $uri $page_type {
    /about about_page;
    ~^/about/us about_us_page;
    default other_page;
}
What will be the value of $page_type for the request URI /about/us?
Aabout_page
Babout_us_page
Cother_page
Dempty string
Attempts:
2 left
💡 Hint
The map directive evaluates keys in the order they appear; the first matching key wins.