51Degrees Device Detection Nginx  4.3

A device detection module for Nginx


This example shows how to return Javascript to obtain further evidence 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.

Properties required for this example are not included in the free Lite file, so a data file that includes ScreenPixelsWidth and ScreenPixelsWidthJavascript need to be obtained from configurator.

Before using the example, update the followings:

  • Remove this 'how to' guide block.
  • Update the file path specifed in 51D_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 overrides in the Nginx document_root.

In a Linux environment, once Nginx has started, open the index.html in a browser.

The index.html send a request to the /51D.js endpoint to obtain the javascript and execute it as below:

<script src="http://localhost:8888/51D.js"></script>

Then use the obtained screen pixels width to send back to the /overrides endpoint. The full details can be seen in the index.html.

The Javascript can be seen by running the below command.

$ curl localhost:8080/51D.js -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 display after open the index.html in the browser is:

Screen Pixels Width: 1920

The number should be the screen pixels width of your machine.

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

worker_processes 4;
load_module modules/ngx_http_51D_module.so;
events {
worker_connections 1024;
# // Snippet Start
http {
## Set the data file for the 51Degrees module to use ##
51D_file_path ./device-detection-cxx/device-detection-data/51Degrees-LiteV4.1.hash;
server {
listen 8888;
location /overrides {
## Get screen pixels width. Use matched all so override evidence can be processed ##
51D_match_all x-screenpixelswidth ScreenPixelsWidth;
## Add to response headers for easy viewing. ##
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Heaaders *;
add_header Access-Control-Expose-Headers *;
add_header x-screenpixelswidth $http_x_screenpixelswidth;
location /51D.js {
## Get the javascript from ScreenPixelsWidthJavascript property ##
51D_get_javascript_single ScreenPixelsWidthJavascript;
## Since the URL location does not actually exist, an error will be logged, ##
## disable file not found error logging ##
log_not_found off;
# // Snippet End