0
0
Nginxdevops~5 mins

Map directive for variable mapping in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to change or assign values based on other values in your web server. The map directive in nginx helps you create new variables by matching existing ones to new values. This makes your server smarter and more flexible without complex code.
When you want to set different backend servers based on the requested domain name.
When you need to assign custom headers depending on the user agent of the visitor.
When you want to enable or disable features based on the client IP address.
When you want to rewrite URLs differently depending on the request path.
When you want to simplify complex if-else logic by mapping values in one place.
Config File - nginx.conf
nginx.conf
http {
    map $http_user_agent $is_mobile {
        default 0;
        ~*Mobile 1;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            if ($is_mobile) {
                return 302 /mobile.html;
            }
            return 200 'Welcome to the desktop site!';
        }
    }
}

The map block creates a new variable $is_mobile based on the $http_user_agent header.

If the user agent contains the word 'Mobile' (case-insensitive), $is_mobile is set to 1, otherwise 0.

Inside the server block, nginx checks $is_mobile to redirect mobile users to a mobile page.

Commands
This command tests the nginx configuration file for syntax errors before applying it.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads nginx to apply the new configuration without stopping the server.
Terminal
systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command sends a request to the server to check the response headers and verify redirection for mobile user agents.
Terminal
curl -I http://example.com/
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Content-Type: text/html Content-Length: 27 Connection: keep-alive
This command simulates a mobile user agent to check if the server redirects to the mobile page.
Terminal
curl -I -A "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) Mobile" http://example.com/
Expected OutputExpected
HTTP/1.1 302 Found Server: nginx Location: /mobile.html Content-Length: 0 Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: the map directive creates new variables by matching existing values, making nginx configuration simpler and more powerful.

Common Mistakes
Using the map directive inside the server block instead of the http block.
The map directive must be defined in the http context; placing it inside server causes nginx to fail loading the config.
Always place the map directive inside the http block at the top level of the configuration.
Not testing the configuration with 'nginx -t' before reloading.
Reloading with syntax errors causes nginx to fail or revert to the old config, leading to downtime or unexpected behavior.
Run 'nginx -t' to verify syntax before reloading the server.
Using incorrect regex syntax in the map keys, like missing ~* for case-insensitive matching.
Without proper regex flags, the map may not match intended values, causing wrong variable assignments.
Use '~*' before regex patterns for case-insensitive matching in map keys.
Summary
The map directive creates new variables by matching existing ones to values.
Define map blocks inside the http context for proper nginx configuration.
Test your configuration with 'nginx -t' before reloading to avoid errors.