A device detection module for Nginx

ipi/gettingStarted.conf

This example shows how to quickly start using 51Degrees' on-premise IP intelligence in Nginx.

This example shows how to quickly start using 51Degrees' on-premise IP intelligence in Nginx. This example is available in full on GitHub.

This example uses the AsnName property which is available in the 51Degrees-IPIV4AsnIpiV41.ipi data file included in the ip-intelligence-data sub-module. When using a data file with a fuller set of properties, such as the Lite or Enterprise files, properties like RegisteredName, RegisteredCountry and RegisteredOwner can be requested in the same way.

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 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 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 %%MODULE_PATH%% with the actual path.
  • Remove the %%TEST_GLOBALS_HTTP%%.
  • Update the %%FILE_PATH_IPI%% 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 ipi in the Nginx document_root.

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

$ curl "localhost:8080/ipi?client_ip=212.58.224.22" -I

Expected output:

HTTP/1.1 200 OK
...
x-asn: <the ASN name of the network the client IP belongs to>
x-asn-query: \"BBC\":1
...

Values are returned in the form "value":weight where the weight indicates the confidence the engine has in the value.

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_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 IP intelligence data file for the 51Degrees module to use. ##
## Update the FILE_PATH_IPI before running with Nginx. ##
51D_file_path_ipi %%FILE_PATH_IPI%%;
server {
listen 127.0.0.1:8080;
server_name localhost;
location /ipi {
## 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-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