Aller au contenu

$os_version

The $os_version variable in NGINX is set by the device-type module. It holds the version of the operating system detected from the client's request. This variable is populated when the module successfully identifies the OS from the request headers.

Type

string

Possible values

  • 14
  • 17.2
  • 11

These values represent the version numbers of various operating systems, such as Android, iOS, or other platforms.

Examples

Logging the OS Version

To log the operating system version in your access logs, you can include $os_version in a custom log_format.

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

    access_log /var/log/nginx/access.log main;
}

Adding OS Version to Response Headers

You can add the OS version to the response headers for debugging or analytics purposes.

server {
    location / {
        add_header X-OS-Version $os_version;
        proxy_pass http://backend;
    }
}

Cache Key Composition

Use the OS version as part of the cache key to differentiate cached content by OS version.

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|$os_version";
            proxy_pass http://backend;
        }
    }
}