Skip to content

$os_name

The $os_name variable holds the name of the operating system detected from the HTTP request. It is set by the device-type module when a request is processed.

Type

string

Possible values

  • Windows: Represents the Windows operating system.
  • Android: Represents the Android operating system.
  • iOS: Represents the iOS operating system.

Examples

Using $os_name in a map for Custom Headers

You can use the $os_name variable in a map to set a custom header based on the operating system.

map $os_name $os_header {
    default "";
    "Windows" "X-OS: Windows";
    "Android" "X-OS: Android";
    "iOS" "X-OS: iOS";
}

server {
    location / {
        add_header $os_header;
        proxy_pass http://backend;
    }
}

Logging the Operating System in log_format

Include the operating system name in your access logs for analytics.

log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$os_name"';

server {
    access_log /var/log/nginx/access.log custom;
    location / {
        proxy_pass http://backend;
    }
}

Conditional Access Control Based on OS

Restrict access to certain content based on the operating system.

server {
    location /restricted-content {
        if ($os_name = "Windows") {
            return 403 "Access denied for Windows users";
        }
        proxy_pass http://backend;
    }
}