\r\n

51Degrees Device Detection Nginx  4.4

A device detection module for Nginx

hash/responseHeaders/responseHeader.conf

This example shows how to obtain response header for 51Degrees' on-premise device detection in Nginx when Client Hints is enabled. This example is available in full on GitHub.

This example requires a local data file. The free 'Lite' data file can be acquired by pulling the git submodules under this repository (run `git submodule update --recursive`) or from the device-detection-data GitHub repository.

The Lite data file is only used for illustration, and has limited accuracy and capabilities. Find out about the more capable data files that are available on our pricing page

Make sure to include the 'BrowserName','BrowserVendor', 'BrowserVersion','HardwareName','HardwareVendor', 'PlatformName','PlatformVendor','PlatformVersion' properties.

Before using the example, update the followings:

  • Remove this 'how to' guide block.
  • Update the %%DAEMON_MODE%% to 'on' or 'off'.
  • Remove the %%TEST_GLOBALS%%.
  • Update the %%MODULE_PATH%% with the actual path.
  • Remove the %%TEST_GLOBALS_HTTP%%.
  • Update the %%FILE_PATH%% with the actual file path.
  • Replace the nginx.conf with this file or run Nginx with -c option pointing to this file.
  • Added the included response.html to the Nginx document_root.

Once Nginx has started, run the following command:

curl -H "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"" http://localhost:8080/response.html

Expected output:

...
<strong>Hardware Vendor: </strong> Unknown
<br />
<strong>Hardware Name: </strong> Desktop|Emulator
<br />
<strong>Device Type: </strong> Desktop
<br />
<strong>Platform Vendor: </strong> Unknown
<br />
<strong>Platform Name: </strong> Unknown
<br />
<strong>Platform Version: </strong> Unknown
<br />
<strong>Browser Vendor: </strong> Google
<br />
<strong>Browser Name: </strong> Chrome
<br />
<strong>Browser Version: </strong> 90
...

This shows that 51Degrees module can perform detection on Client Hints without User-Agent.

Run the command again with -I flag:

curl -H "sec-ch-ua: \" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"" http://localhost:8080/response.html -I

Expected output:

Accept-CH: SEC-CH-UA,SEC-CH-UA-Full-Version,SEC-CH-UA-Model,SEC-CH-UA-Mobile,SEC-CH-UA-Arch,SEC-CH-UA-Platform,SEC-CH-UA-Platform-Version

This shows that after a detection, 51Degrees module will construct response headers, indicating the Client Hints that it needs in order to perform more detection where is applicable..

User agents that support Client Hints will process the response headers and send further Client Hints in the subsequent requests. With further Client Hints, 51Degrees module will be able to detect more properties of the sending device.

User can also open the following url in a browser that supports Client Hints and follows the displayed instructions.

http://localhost:8080/response.html

NOTE: All the lines above, this line and the end of comment block line after this line should be removed before using this example.

## Update DAEMON_NODE to 'on' or 'off' before running with Nginx ##
daemon %%DAEMON_MODE%%;
worker_processes 4;
## The following line is only for testing. Remove before ##
## running with Nginx ##
%%TEST_GLOBALS%%
## Update MODULE_PATH before running with Nginx ##
load_module %%MODULE_PATH%%modules/ngx_http_51D_module.so;
events {
worker_connections 1024;
}
# // Snippet Start
http {
## The following line is only for testing. Remove before ##
## running with Nginx ##
%%TEST_GLOBALS_HTTP%%
## Set the data file for the 51Degrees module to use ##
## Update the FILE_PATH before running with Nginx ##
51D_file_path %%FILE_PATH%%;
server {
listen 127.0.0.1:8080;
server_name localhost;
location /response.html {
# Enable the response headers to be set for Client-Hints support.
51D_set_resp_headers on;
# Request browserVersion and IsMobile
51D_match_all x-hardwarevendor HardwareVendor;
51D_match_all x-hardwarename HardwareName;
51D_match_all x-devicetype DeviceType;
51D_match_all x-platformvendor PlatformVendor;
51D_match_all x-platformname PlatformName;
51D_match_all x-platformversion PlatformVersion;
51D_match_all x-browservendor BrowserVendor;
51D_match_all x-browsername BrowserName;
51D_match_all x-browserversion BrowserVersion;
# Replace the content in returned html with found one.
sub_filter 'HardwareVendor' $http_x_hardwarevendor;
sub_filter 'HardwareName' $http_x_hardwarename;
sub_filter 'DeviceType' $http_x_devicetype;
sub_filter 'PlatformVendor' $http_x_platformvendor;
sub_filter 'PlatformName' $http_x_platformname;
sub_filter 'PlatformVersion' $http_x_platformversion;
sub_filter 'BrowserVendor' $http_x_browservendor;
sub_filter 'BrowserName' $http_x_browsername;
sub_filter 'BrowserVersion' $http_x_browserversion;
}
}
}
# // Snippet End