\r\n

51Degrees Device Detection Nginx  4.4

A device detection module for Nginx

hash/gettingStarted.conf

This example shows how to quickly start using 51Degrees' on-premise device detection in Nginx. 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 at least IsMobile, BrowserName and PlatformName properties for this to work.

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.
  • Create a static file hash in the Nginx document_root.

In a Linux environment, once Nginx has started, run the following command:

$ curl localhost:8080/hash -I -A "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53"

Expected output:

HTTP/1.1 200 OK
...
x-device: Mobile Safari,iOS
x-mobile: True
...

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

## Replace DAEMON_MODE with 'on' or 'off' before running ##
## with Nginx. ##
daemon %%DAEMON_MODE%%;
worker_processes 4;
## The following line is required 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 required 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 /hash {
## Do a single User-Agent match for device information ##
51D_match_ua x-device BrowserName,PlatformName;
## Do a User-Agent + Client-Hints match for device information ##
51D_match_ua_client_hints x-device-hinted BrowserName,PlatformName;
## Do a multiple HTTP header match for IsMobile ##
51D_match_all x-mobile IsMobile;
## Add to response headers for easy viewing. ##
add_header x-device $http_x_device;
add_header x-device-hinted $http_x_device_hinted;
add_header x-mobile $http_x_mobile;
}
}
}
# // Snippet End