Pular para conteúdo

$is_mobile

The $is_mobile variable is set by the device-type module to indicate whether a request originates from a phone or mobile-only browser. It helps in distinguishing mobile traffic from other types of devices.

Type

boolean (1/0)

Possible values

  • 1: The request is from a mobile device.
  • 0: The request is not from a mobile device.

Examples

Adaptive Content Serving

Use $is_mobile to serve different content based on the device type.

server {
    location / {
        if ($is_mobile) {
            rewrite ^(.*)$ /mobile$1 last;
        }
        proxy_pass http://backend;
    }
}

Cache Key Variation

Modify cache keys to differentiate between mobile and non-mobile requests.

http {
    proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m;

    server {
        location / {
            proxy_cache my_cache;
            proxy_cache_key "$scheme$request_uri|$is_mobile";
            proxy_pass http://backend;
        }
    }
}

Logging Device Type

Log requests with an indication of whether they are from mobile devices.

http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$is_mobile"';

    server {
        access_log /var/log/nginx/access.log main;

        location / {
            proxy_pass http://backend;
        }
    }
}