What is the purpose of the map directive in nginx?
The map directive creates a new variable whose value depends on the value of another variable. It helps to simplify complex conditional logic by mapping input values to output values.
Click to reveal answer
beginner
How do you define a basic map block in nginx?
You define it with map $input_variable $output_variable { ... } where inside the braces you list input values and their corresponding output values.
Click to reveal answer
intermediate
What happens if no match is found in a map directive?
If no match is found, the map directive uses the value defined by the default keyword if provided, or an empty string otherwise.
Click to reveal answer
intermediate
Can the map directive use regular expressions for matching?
Yes, the map directive supports regular expression matching by prefixing the pattern with ~ for case-sensitive or ~* for case-insensitive matching.
Click to reveal answer
beginner
Where should the map directive be placed in the nginx configuration?
The map directive must be placed in the http context, outside of server or location blocks.
Click to reveal answer
What does the map directive in nginx do?
AMaps one variable's value to another variable's value
BDefines server listening ports
CSets up SSL certificates
DConfigures proxy pass
✗ Incorrect
The map directive creates a new variable based on the value of another variable.
Where must the map directive be placed in nginx config?
AInside <code>location</code> block
BInside <code>server</code> block
CInside <code>events</code> block
DInside <code>http</code> block
✗ Incorrect
The map directive must be placed in the http context.
How do you specify a default value in a map directive?
AUsing the <code>default</code> keyword
BUsing <code>else</code> keyword
CUsing <code>fallback</code> keyword
DUsing <code>catch</code> keyword
✗ Incorrect
The default keyword sets the value if no other match is found.
Which prefix is used for case-insensitive regex matching in map?
A~
B~*
C^~
D*~
✗ Incorrect
The ~* prefix is used for case-insensitive regex matching.
What will be the output variable if no match and no default is set in map?
AError thrown
BInput variable value
CEmpty string
DNull value
✗ Incorrect
If no match and no default, the output variable is an empty string.
Explain how the map directive works in nginx and give an example of its usage.
Think about how you can assign values conditionally without if statements.
You got /4 concepts.
Describe where and why you would use the map directive in an nginx configuration.
Consider how nginx handles variables and conditions.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the map directive in nginx configuration?
easy
A. To create new variables based on existing variables
B. To define server blocks for different domains
C. To set up SSL certificates
D. To configure load balancing between servers
Solution
Step 1: Understand the role of map in nginx
The map directive is used to create new variables by mapping values from existing variables.
Step 2: Compare with other nginx directives
Other options like server blocks, SSL, and load balancing serve different purposes unrelated to variable mapping.
Final Answer:
To create new variables based on existing variables -> Option A
Quick Check:
map creates variables [OK]
Hint: Map creates variables from existing ones [OK]
Common Mistakes:
Confusing map with server block configuration
Thinking map sets SSL or load balancing
Assuming map modifies request headers directly
2. Which of the following is the correct syntax to start a map block in nginx?
easy
A. map ($variable, $new_variable) {
B. map $new_variable $variable {
C. map { $variable $new_variable }
D. map $variable $new_variable {
Solution
Step 1: Recall the syntax of the map directive
The correct syntax is map $variable $new_variable { where the first variable is the source and the second is the target.
Step 2: Eliminate incorrect syntax
The incorrect options use wrong order or invalid punctuation for nginx configuration.
What will be the value of $is_mobile if the user agent is Mozilla/5.0 (Linux; Mobile)?
medium
A. Mobile
B. 0
C. 1
D. default
Solution
Step 1: Understand the map pattern matching
The map uses a case-insensitive regex ~*Mobile to match user agents containing 'Mobile'.
Step 2: Check the user agent string
The user agent contains 'Mobile', so the regex matches and sets $is_mobile to 1.
Final Answer:
1 -> Option C
Quick Check:
Regex match 'Mobile' sets value to 1 [OK]
Hint: Regex ~*Mobile matches user agent containing 'Mobile' [OK]
Common Mistakes:
Ignoring case-insensitive regex (~*)
Assuming default value applies despite match
Confusing variable names
4. Identify the error in this nginx map configuration:
map $request_method $allow_method {
GET yes;
POST yes;
DELETE no
}
medium
A. Missing semicolon after DELETE no
B. Incorrect variable names
C. map directive cannot map request methods
D. Using yes/no instead of 1/0 is invalid
Solution
Step 1: Check syntax for each mapping line
Each line inside map must end with a semicolon. The line DELETE no is missing a semicolon.
Step 2: Validate other options
Variable names are valid, mapping request methods is allowed, and yes/no strings are acceptable values.
Final Answer:
Missing semicolon after DELETE no -> Option A
Quick Check:
Each map line ends with ; [OK]
Hint: Every map entry line ends with a semicolon [OK]
Common Mistakes:
Omitting semicolons at line ends
Thinking yes/no are invalid values
Assuming map can't use request methods
5. You want to create a variable $cache_key that is mobile if the user agent contains 'Mobile', otherwise desktop. Which map configuration correctly achieves this?
hard
A. map $cache_key $http_user_agent {
mobile ~*Mobile;
desktop default;
}
B. map $http_user_agent $cache_key {
~*Mobile mobile;
default desktop;
}
C. map $http_user_agent $cache_key {
default desktop
~*Mobile mobile
}
D. map $http_user_agent $cache_key {
Mobile mobile;
default desktop;
}
Solution
Step 1: Confirm correct map syntax and order
The map directive must have source variable first, target variable second, and each line ends with semicolon.
Step 2: Check pattern matching and default
map $http_user_agent $cache_key {
~*Mobile mobile;
default desktop;
} uses case-insensitive regex ~*Mobile to match user agents containing 'Mobile' and sets mobile, else desktop.
Step 3: Eliminate incorrect options
map $cache_key $http_user_agent {
mobile ~*Mobile;
desktop default;
} reverses variables, one misses semicolons, another uses exact 'Mobile' without regex and may miss case-insensitive matches.