Nginx map and geo: Turn Inputs into Controlled Labels
Choose from a fixed set of values
map declares a derived variable in the http context. It lets configuration choose a result from a lookup table rather than repeating conditions in every location. In this example a harmless request header selects a display theme; the mapped result is only one of a few fixed labels. It does not grant privileges.
map $http_x_lab_theme $theme { default paper; dark midnight; "" paper; ~^preview-[0-9]+$ preview;}location = /theme { default_type text/plain; return 200 "$theme\n";}A header of DARK produces midnight, preview-7 produces preview, and Preview-7 produces paper. Literal string keys ignore case, while ~ starts a case-sensitive regex and ~* starts an insensitive one. Missing and unmatched inputs fall back to paper here. Without an explicit default, an unmatched map value is empty. Keep the allowed output small and meaningful.
Resolve overlapping matches deliberately
map $host $site_group { hostnames; default other; api.studio.test api; *.studio.test studio; ~^demo[0-9]+[.]test$ demo;}Exact literals take precedence over hostname masks; within supported mask forms the longest applicable mask wins, with prefix masks considered before suffix masks. Ordered regex rules are considered after literal and hostname matches. The first matching regex wins. Thus api.studio.test selects api, not studio. hostnames enables the special masks and should precede the entries.
map $http_x_lab_theme $preview_label { default none; ~^preview-(?<preview_number>[0-9]+)$ build-$preview_number;}For preview-42 the value is build-42. Captures contain text extracted from the input; they are not validated identifiers merely because a pattern matched. Positional captures can be replaced by later regex matches, so do not assume $1 keeps its value forever. Here the derived label is used only for a benign display example, not a filesystem path or URL destination.
Classify IP prefixes with geo
geo $arg_sample $network_label { default outside; 192.0.2.0/24 demo; 192.0.2.64/26 special; 2001:db8::/32 demo-v6;}This educational table uses documentation-only IP ranges, not geographical coordinates. The optional source variable is a query argument so the learner can test a label without changing network interfaces. Without that optional input, geo uses $remote_addr. For CIDR tables the most specific matching prefix wins: 192.0.2.70 selects special, 192.0.2.8 selects demo, and 2001:db8::5 selects demo-v6. Invalid input is treated as 255.255.255.255 by the module, so it selects outside in this table. An unmatched address uses the default (empty if omitted). Never use query-supplied IPs for access decisions.
curl --noproxy '*' --max-time 5 'http://127.0.0.1:18141/network?sample=192.0.2.70'The expected body is special plus a newline when the complete lab is running. The fixture uses no external IP lookup, location database or public service. geo also supports range mode and trusted-proxy settings, but those require separate configuration review; do not enable trust for arbitrary forwarding sources.
Practice defaults and precedence
Evaluate the theme and network table for normal, missing, mixed-case and invalid values. Add an exact hostname that also matches a wildcard and explain the result before testing it. Next, learn how a variable can remain cached after its first evaluation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Map and geo decisions
Choose the best answer for the chapter examples.
What does the theme map return for DARK?
What does the theme map return for Preview-7?
api.studio.test matches both an exact entry and a wildcard. Which label wins?
Which network label matches 192.0.2.70?
Why is the sample query address unsuitable for authorization?