$device_type
The $device_type variable is set by the device-type module in NGINX. It holds the primary classification of the device making the HTTP request, such as whether the request originates from a mobile device, desktop, or other types.
Type
string
Possible values
mobile: Indicates a mobile device such as a smartphone.tablet: Indicates a tablet device.desktop: Indicates a desktop or laptop computer.tv: Indicates a smart TV.console: Indicates a gaming console.car: Indicates an in-car system.wearable: Indicates a wearable device like a smartwatch.camera: Indicates a camera device.peripheral: Indicates a peripheral device.bot: Indicates a bot or crawler.
Examples
Using $device_type in a map
Create a map to set a variable based on the device type for adaptive content serving.
map $device_type $content_variant {
default "desktop";
mobile "mobile";
tablet "tablet";
}
server {
location / {
proxy_pass http://backend;
proxy_set_header X-Content-Variant $content_variant;
}
}
Adding a header with $device_type
Add a custom header to responses to indicate the device type.
server {
location / {
add_header X-Device-Type $device_type;
proxy_pass http://backend;
}
}
Cache key composition using $device_type
Vary the cache key by device type to ensure device-specific content is cached separately.
server {
location / {
proxy_cache_key "$scheme$request_uri|$device_type";
proxy_pass http://backend;
}
}