\r\n

51Degrees Device Detection Nginx  4.3

A device detection module for Nginx

hash/config.conf

This example shows how to configure the available settings for 51Degrees' on-premise device detection in Nginx. This example is available in full on GitHub.

This example requires a local data file. Free data files can be acquired by pulling the submodules under this repository or from the device-detection-data GitHub repository.

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 config in the Nginx document_root.

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

$ curl localhost:8080/config -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^sep^iOS^sep^PERFORMANCE
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 the DAEMON_MODE with 'on' or 'off'. ##
daemon %%DAEMON_MODE%%;
worker_processes 4;
## The following line is required for testing. Remove before ##
## running with Nginx. ##
%%TEST_GLOBALS%%
## Update the 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%%;
51D_value_separator ^sep^;
51D_drift 3;
51D_difference 5;
51D_allow_unmatched on;
51D_use_performance_graph on;
51D_use_predictive_graph off;
server {
listen 127.0.0.1:8080;
server_name localhost;
location /config {
## Do a single User-Agent match for device information ##
51D_match_single x-device BrowserName,PlatformName,Method;
## 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-mobile $http_x_mobile;
}
}
}
# // Snippet End