A device detection module for Nginx

mixed/gettingStarted.conf

This example shows how to use 51Degrees' on-premise device detection and IP intelligence together in the same Nginx configuration.

This example shows how to use 51Degrees' on-premise device detection and IP intelligence together in the same Nginx configuration. This example is available in full on GitHub.

Both modules are loaded and each engine is given its own data file. The device detection matches use the request headers whilst the IP intelligence matches use the client IP address, or an IP address supplied in a query argument.

Make sure to include at least IsMobile for device detection and AsnName for IP intelligence for this to work. The AsnName property is available in the 51Degrees-IPIV4AsnIpiV41.ipi data file included in the ip-intelligence-data sub-module.

When running on a local machine the client IP address reported by Nginx is a loopback or private network address, which has no useful IP intelligence associated with it. The second IP intelligence match below therefore reads a real IP address from the client_ip query string argument via the $arg_client_ip variable, overriding the reported client IP address for that match. This is the mechanism used by the tests. In production the variable argument can be omitted, as in the first IP intelligence match below, so the client IP address is used directly.

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 two %%MODULE_PATH%% with the actual path.
  • Remove the %%TEST_GLOBALS_HTTP%%.
  • Update the %%FILE_PATH%% and %%FILE_PATH_IPI%% with the actual file paths.
  • Replace the nginx.conf with this file or run Nginx with -c option pointing to this file.
  • Create a static file mixed in the Nginx document_root.

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

$ curl "localhost:8080/mixed?client_ip=212.58.224.22" -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-mobile: True
x-asn: <the ASN name of the network the client IP belongs to>
x-asn-query: \"BBC\":1
...

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;
load_module %%MODULE_PATH%%modules/ngx_http_51D_ipi_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 each engine to use. ##
## Update the FILE_PATH and FILE_PATH_IPI before running with Nginx. ##
51D_file_path %%FILE_PATH%%;
51D_file_path_ipi %%FILE_PATH_IPI%%;
server {
listen 127.0.0.1:8080;
server_name localhost;
location /mixed {
## Do a multiple HTTP header match for IsMobile ##
51D_match_all x-mobile IsMobile;
## Do an IP intelligence match on the client IP address ##
51D_match_ipi x-asn AsnName;
## Do an IP intelligence match on the IP address passed ##
## in the 'client_ip' query argument ##
51D_match_ipi x-asn-query AsnName $arg_client_ip;
## Add to response headers for easy viewing. ##
add_header x-mobile $http_x_mobile;
add_header x-asn $http_x_asn;
add_header x-asn-query $http_x_asn_query;
}
}
}
# // Snippet End
ngx_module_t ngx_http_51D_ipi_module
Global module declaration.
Definition ngx_http_51D_ipi_module.c:97
ngx_module_t ngx_http_51D_module
Global module declaration.
Definition ngx_http_51D_module.c:117