$device_json
The $device_json variable holds a complete detection result in JSON format. It is set by the device-type module for each HTTP request, providing detailed information about the device, browser, operating system, and bot classification.
Type
JSON
Possible values
The $device_json variable contains a JSON object with detailed device detection information. Example values include:
-
For a mobile device:
{ "type": "mobile", "browser": {"name": "Chrome", "version": "120.0", "engine": "Blink"}, "os": {"name": "Android", "version": "14", "family": "Android"}, "device": {"brand": "Google", "model": "Pixel 8"}, "bot": null } -
For a bot:
{ "type": "bot", "browser": {"name": "", "version": "", "engine": ""}, "os": {"name": "", "version": "", "family": ""}, "device": {"brand": "", "model": ""}, "bot": {"name": "GPTBot", "category": "ai_crawler", "producer": "OpenAI", "is_ai": true} }
Examples
Logging Device Information
To log the complete device detection result, you can include $device_json in your log_format.
http {
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$device_json';
access_log /var/log/nginx/access.log main;
}
Adding Device Information to Response Headers
You can add the device detection result to response headers for debugging or analytics purposes.
server {
location / {
add_header X-Device-Info $device_json;
proxy_pass http://backend;
}
}
Conditional Access Control
Use $device_json to block certain types of devices or bots based on the detection result.
server {
location / {
if ($device_json ~* "ai_crawler") {
return 403 "Access denied for AI crawlers";
}
proxy_pass http://backend;
}
}