\r\n

51Degrees Device Detection Nginx  4.3

A device detection module for Nginx

hash/matchQuery.conf

This example shows how query argument take precendence over header value 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 property 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 query in the Nginx document_root.

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

curl -I "http://localhost:8080/query?User-Agent=Mozilla/5.0%20(iPhone;%20CPU%20iPhone%20OS%207_1%20like%20Mac%20OS%20X)%20AppleWebKit/537.51.2%20(KHTML,%20like%20Gecko)%20Version/7.0%20Mobile/11D167%20Safari/9537.53" -A "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0"

The header is a desktop User-Agent while the argument is a mobile User-Agent. Expected output:

HTTP/1.1 200 OK
...
x-mobile-single: True
x-mobile-all: 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_NODE with '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 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 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 /query {
## Do a single User-Agent match for device information ##
51D_match_single x-mobile-single IsMobile $arg_user_agent;
## Do a multiple HTTP header match for IsMobile ##
51D_match_all x-mobile-all IsMobile;
## Add to response headers for easy viewing. ##
add_header x-mobile-single $http_x_mobile_single;
add_header x-mobile-all $http_x_mobile_all;
}
}
}
# // Snippet End