Bird
Raised Fist0
Nginxdevops~10 mins

Map directive for variable mapping in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Map directive for variable mapping
Start
↓
Define map block
↓
Input variable checked
↓
Match input value to map keys
↓
If match found
→Assign mapped value to new variable
↓
If no match
→Assign default value
↓
Use mapped variable in config
↓
End
The map directive checks an input variable, matches it against keys, assigns a mapped value or default, then uses that mapped variable in configuration.
Execution Sample
Nginx
map $http_user_agent $is_mobile {
    default 0;
    ~*Mobile 1;
}

server {
    if ($is_mobile) { return 302 /mobile; }
}
This code maps user agents containing 'Mobile' to 1, others to 0, then redirects mobile users.
Process Table
StepInput $http_user_agentMap MatchMapped $is_mobileAction
1"Mozilla/5.0 (Mobile)"Matches ~*Mobile1Redirect to /mobile
2"Mozilla/5.0 (Windows NT)"No match, use default0No redirect, serve normally
3"SomeOtherAgent"No match, use default0No redirect, serve normally
💡 All requests processed; mapping assigns 1 for mobile user agents, 0 otherwise.
Status Tracker
VariableStartAfter 1After 2After 3
$http_user_agentN/A"Mozilla/5.0 (Mobile)""Mozilla/5.0 (Windows NT)""SomeOtherAgent"
$is_mobileN/A100
Key Moments - 2 Insights
Why does $is_mobile become 0 for some user agents?
Because the map directive uses 'default 0;' for any input that does not match the regex ~*Mobile, as shown in execution_table rows 2 and 3.
How does the map directive decide which value to assign?
It checks the input variable against each key in order; if a regex or exact match is found, it assigns that value; otherwise, it assigns the default value, as seen in the concept_flow and execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $is_mobile at step 2?
A0
BUndefined
C1
DMobile
💡 Hint
Check the 'Mapped $is_mobile' column at step 2 in the execution_table.
At which step does the input match the regex ~*Mobile?
AStep 3
BStep 2
CStep 1
DNone
💡 Hint
Look at the 'Map Match' column in the execution_table.
If the default value in the map was changed to 2, what would $is_mobile be at step 3?
A0
B2
C1
DUndefined
💡 Hint
Refer to the 'Map Match' and 'Mapped $is_mobile' logic in the execution_table and variable_tracker.
Concept Snapshot
map directive syntax:
map $input_var $output_var {
  default value;
  key1 value1;
  ~regex value2;
}

It maps input variable values to output variable values.
If no key matches, default is used.
Useful for conditional config based on variables.
Full Transcript
The nginx map directive lets you create a new variable based on the value of an existing variable. You define a map block with keys and values. When nginx processes a request, it checks the input variable against the keys. If it finds a match, it assigns the corresponding value to the new variable. If no match is found, it assigns the default value. This is useful to simplify conditional logic in your configuration. For example, mapping user agents containing 'Mobile' to 1 and others to 0, then redirecting mobile users. The execution table shows how different user agents are mapped and what actions follow. The variable tracker shows how variables change per request. Understanding the map directive helps you write cleaner, more efficient nginx configs.

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

  1. Step 1: Understand the role of map in nginx

    The map directive is used to create new variables by mapping values from existing variables.
  2. Step 2: Compare with other nginx directives

    Other options like server blocks, SSL, and load balancing serve different purposes unrelated to variable mapping.
  3. Final Answer:

    To create new variables based on existing variables -> Option A
  4. 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

  1. 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.
  2. Step 2: Eliminate incorrect syntax

    The incorrect options use wrong order or invalid punctuation for nginx configuration.
  3. Final Answer:

    map $variable $new_variable { -> Option D
  4. Quick Check:

    map syntax = map $source $target { [OK]
Hint: Map syntax: map $source $target { [OK]
Common Mistakes:
  • Swapping source and target variables
  • Using parentheses or braces incorrectly
  • Missing the $ sign before variables
3. Given this nginx map configuration:
map $http_user_agent $is_mobile {
    default 0;
    ~*Mobile 1;
}

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

  1. Step 1: Understand the map pattern matching

    The map uses a case-insensitive regex ~*Mobile to match user agents containing 'Mobile'.
  2. Step 2: Check the user agent string

    The user agent contains 'Mobile', so the regex matches and sets $is_mobile to 1.
  3. Final Answer:

    1 -> Option C
  4. 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

  1. 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.
  2. Step 2: Validate other options

    Variable names are valid, mapping request methods is allowed, and yes/no strings are acceptable values.
  3. Final Answer:

    Missing semicolon after DELETE no -> Option A
  4. 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

  1. 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.
  2. 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.
  3. 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.
  4. Final Answer:

    map $http_user_agent $cache_key { ~*Mobile mobile; default desktop; } -> Option B
  5. Quick Check:

    Correct map syntax with regex and default [OK]
Hint: Use map $source $target with regex and default [OK]
Common Mistakes:
  • Swapping source and target variables
  • Missing semicolons after lines
  • Not using case-insensitive regex for matching
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard