• About Us
  • Blog
  • Basket
  • Account
  • Sign In


How to Build 51Degrees Device Detection as an NGINX Dynamic Module

Published on Friday, February 23, 2018

How to Build 51Degrees Device Detection as an NGINX Dynamic Module

In this tutorial video we show you how to build 51Degrees Device Detection as an NGINX Dynamic Module. 

51Degrees are certified as an NGINX module. We thought we’d put together a tutorial showing how you can integrate our Device Detection API into your NGINX Server. In this video we go through how to set up our device detection solution on a Linux machine using NGINX, add properties to call from the data file and finally check that the results are being returned as response headers on the site.


This video was put together by Josh, one of our engineers at 51Degrees.

In this video we will go through the initial setup, adding properties to call from the data file and finally checking that the results are being returned as response headers on the site.

We don’t cover installing NGINX but here is a link with guidelines on how to build it from source.

With version 1.11.5 NGINX introduced the capability to compile dynamic modules independently which is what we will be doing.

It is possible to install Device Detection using an earlier version but it’s not something we will cover in this tutorial. If you need any help you can get in touch with us.

It’s important to check you have ‘—with-compat’ available as a configure argument. You can check this by doing:

Nginx -V

From there you can see the version of NGINX you are running as well as all the configure arguments.

Downloading and building the dynamic module

The first thing you are going to need to do is download our Repository. 51Degrees is an open source product so you can get this by going to our GitHub.

git clone https://github.com/51Degrees/Device-Detection.git


Copy over your 51Degrees data file into the cloned repository.

cp path/to/51Degreesdatafile.trie Device-Detection/data


Move into the 51Degrees NGINX directory.

cd Device-Detection/nginx/


NOTE: The next few steps are not required to install as a dynamic module but is recommended as it contains tests that allow you to see any potential issues during install. You will need to edit the Makefile so that the tests are referencing the correct data file.

nano Makefile


Dependent on whether you are using Pattern or Hash Trie, change the name of the data file in code below and save.

ifeq (test,$(firstword $(MAKECMDGOALS)))
FULLPATH := $(shell pwd)
ifneq (,$(findstring TRIE,"$(shell ./nginx -V 2>&1)"))
        DATAFILE := "51Degrees-LiteV3.4.trie"
ifneq (,$(findstring PATTERN,"$(shell ./nginx -V 2>&1)"))
        DATAFILE := "51Degrees-LiteV3.2.dat"


NOTE: replace trie with pattern if using this detection method.

make install trie


make test


Now onto to building the dynamic module, replace the version in the example below with the current version of NGINX you are using.

make module trie VERSION=1.13.7


Configuring NGINX with 51Degrees


Copy the output to your NGINX directory. If you do not have a modules folder then you can just create one.

sudo cp build/modules/ngx_http_51D_module.so /path/to/nginx/prefix/modules/


Copy your data file to a location where NGINX get to it. In the video example we have created a data folder within the Prefix location.

sudo cp ../../../data/51Degreesdatafile.trie /path/to/nginx/prefix/data/


Amend your Nginx config file.

sudo nano /path/to/nginx/prefix/nginx.conf


Add a reference to your compiled dynamic module near the top of the config.

load_module modules/ngx_http_51D_module.so;


Add some settings in the config file. NOTE: The cache setting shown below is purely for Pattern.

http {

    51D_filePath /path/to/nginx/data/51Degreesdatafile.trie;
	51D_cache 10000;
	#Additional settings


Within the Location block, add in some matches and add them to return as response headers.

 location / {
            root   html;
            index  index.html index.html;

            51D_match_all x-mobile IsMobile;
            51D_match_all x-tablet IsTablet;

            add_header x-mobile $http_x_mobile;
            add_header x-tablet $http_x_tablet;


Check everything over and then save.

nginx -t


restart NGINX

sudo systemctl restart nginx

Now configuration is complete, you can connect to your web server via an Internet browser and open up the developer tools. If you reload the page when on the network tab you will be able to see the 2 matches we have created returning as response headers.

Try emulating a few different devices to see the results.

Note: If you use a tablet, x-mobile will return true as, within our data base, we count tablets as "mobile" because you can use it while you are walking down the street. if you were to call the "IsSmartphone" property instead of "IsMobile" then a tablet would return as false.

We hope you found this information helpful.

Please feel free to contact us with any question.

Try 51Degrees today to add detailed device information to your website

Get Started

Comments (0)
Chris Rudwick

Chris Rudwick

Other posts by Chris Rudwick
Contact author



.NET 2013 2014 4G 51Degrees 5G A.C.Roma A7 ABI Acer Adtech Affiliate Marketing Afilias Alcatel Amazon AMP Analysis Analytics Android Android 5.0 Lollipop Android Kitkat Android Lollipop Android Media Stick Apache API Apple Apple TV Archos Asha Asian Market ASP.NET Asus Australia Big Data Black Friday Blackberry Blink Browser C C# Case Study CeBIT CES Chrome Cloud CMS combinations Comparison Competition CoolPad COTW Cron CSS3 Data Data Blog Data File Data Model Daydream Denver Design Desire Eye Desktop Detection Developers Device Device Data Device Detection Device Intelligence Device Popularity Device property Device Types Device Use DeviceAtlas Display dmexco DoCoMo Doogee DotNetNuke Download Drupal Email EReader E-Reader Ericsson Evaluation Event Examples EXPLAY Rio Facebook feature Firefox Firefox OS Fly Foundation Framework France Galaxy S3 Galaxy S5 Galaxy Tab A Galaxy Tab A 8.0 Galaxy Tab A 9.7 Germany git repositories Global Google Google Daydream GSMA HAProxy Hardware Hisense HTC HTC ONE MAX HTC OS HTML5 HTTP HTTP Headers Huawei HUAWEI. UPDATE HUDL Huwaei IBC Icemobile Prime 4.0 IE IFA IIS Image Optimiser Image Optimizer India Infographic Ingeniux Internet usage iOS iOS 7 iOS 8 ipad iPhone iPhone 6 IsEmailBrowser IsWebApp Italy Japan Java Javascript Jolla Kentico Keynote Kindle Kindle Fire Kindle Fire HD Leagoo Lenovo LG Location Log File Analysis LTE Lumia m.dot macOS Map Memory Meta Data Mi 4S Micromax Microsoft Miia Style Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Module Motorola MVC4 MWC MWC 2017 MWC16 MyPhone Native Native Apps NET New Release News News Letter Nexus Nexus 6 Nexus 9 NFC NGINX Nokia Nokia 3310 Non-Mobile NVIDIA Omate On7 OnePlus 5 Opera Opera Mini Operating System Optimisation OS OSX 10.10 OTA Panasonic Patent PC Pebble Performance phablet phone PHP Poland Presentation Press Release Price Band PRIV programmatic PS4 Publishers Python QMobile QR Codes Redirection Research Reseller Responsive Images Responsive web design RESS Review reviews RIM Ringmark RTB RWD Safari Samsung Scala ScientiaMobile Screen Screen resolution Screen Size SEO Server Server-side optimisation Set Box Set Top Box Sharepoint Shark 1 SHIFT phones Sitecore Sitecore version 9 SLUSH Smart TV Smartphone Smartphones Smartwatches Snapdragon Sony Sony Xperia Spain Swedish Beers Symbian Tablet Tablets Tesco Testing Tips Top 5 TOTW Tutorial TV UDS UK Umbraco Update updates US User Agent UserAgent User-Agent Vendors Version 3 Video VoLTE VR Wearable Web Web Apps Web content management WebKit WebMatrix White Paper Widgets Widnows WiFi Wiko Wileyfox Windows Windows Phone WURFL Xbox XBox One Xiaomi Xperia Xperia z Yosemite Z10 ZenFon 2 ZOPO ZTE