Device detection
51Degrees
Available since
- HAProxy Enterprise 1.7r2
The 51Degrees module provides device detection services using the 51Degrees database.
Install the 51Degrees module Jump to heading
-
Go to the 51Degrees Downloads page to get the 51Degrees device detection data file.
- For HAProxy Enterprise 3.0r1 and newer, use the V4
.hash
data file. - For HAProxy Enterprise 2.9r1 and older, use the V3
.trie
data file.
- For HAProxy Enterprise 3.0r1 and newer, use the V4
-
Copy the data file to your HAProxy Enterprise server (for example
/etc/hapee-3.0/51Degrees-LiteV4.1.hash
). -
Install the 51Degrees module according to your platform:
nixsudo apt-get install hapee-<VERSION>-lb-51dnixsudo apt-get install hapee-<VERSION>-lb-51dExample for HAProxy Enterprise 3.0r1:
nixsudo apt-get install hapee-3.0r1-lb-51dnixsudo apt-get install hapee-3.0r1-lb-51dnixsudo yum install hapee-<VERSION>-lb-51dnixsudo yum install hapee-<VERSION>-lb-51dExample for HAProxy Enterprise 3.0r1:
nixsudo yum install hapee-3.0r1-lb-51dnixsudo yum install hapee-3.0r1-lb-51dnixsudo zypper install hapee-<VERSION>-lb-51dnixsudo zypper install hapee-<VERSION>-lb-51dExample for HAProxy Enterprise 3.0r1:
nixsudo zypper install hapee-3.0r1-lb-51dnixsudo zypper install hapee-3.0r1-lb-51dnixsudo pkg install hapee-<VERSION>-lb-51dnixsudo pkg install hapee-<VERSION>-lb-51dExample for HAProxy Enterprise 3.0r1:
nixsudo pkg install hapee-3.0r1-lb-51dnixsudo pkg install hapee-3.0r1-lb-51d -
In the
global
section of your configuration, add the following lines. Change the51degrees-property-name-list
depending on the 51Degrees device properties you want to use. Note that some properties become available depending on your 51Degrees pricing tier.haproxyglobalmodule-path /opt/hapee-3.0/modules/module-load hapee-lb-51d.so51degrees-data-file /etc/hapee-3.0/51Degrees-LiteV4.1.hash51degrees-property-name-list DeviceType IsMobile ScreenPixelsHeight ScreenPixelsWidthhaproxyglobalmodule-path /opt/hapee-3.0/modules/module-load hapee-lb-51d.so51degrees-data-file /etc/hapee-3.0/51Degrees-LiteV4.1.hash51degrees-property-name-list DeviceType IsMobile ScreenPixelsHeight ScreenPixelsWidth -
Reload the configuration to apply the changes.
nixsudo systemctl reload hapee-3.0-lbnixsudo systemctl reload hapee-3.0-lb
Use 51Degrees Jump to heading
After installing the module, use the following directives to perform database lookups.
51d.all Jump to heading
Use the fetch method 51d.all
to perform a lookup in the database that returns the values of the specified properties. The function can be passed up to five property names and if a property name can’t be found, it returns the value NoData
.
Syntax
text
51d.all(<prop>[,<prop>*])
text
51d.all(<prop>[,<prop>*])
In this example, we create an HTTP request header that contains device information:
haproxy
frontend wwwbind :80mode httphttp-request set-header X-DeviceInfo %[51d.all(DeviceType,IsMobile,ScreenPixelsHeight,ScreenPixelsWidth)]
haproxy
frontend wwwbind :80mode httphttp-request set-header X-DeviceInfo %[51d.all(DeviceType,IsMobile,ScreenPixelsHeight,ScreenPixelsWidth)]
In the next example, we define an ACL named is_mobile
and then use it when choosing a backend:
haproxy
frontend wwwbind :80mode httpacl is_mobile 51d.all(IsMobile) "True"use_backend mobile_site if is_mobiledefault_backend desktop_site
haproxy
frontend wwwbind :80mode httpacl is_mobile 51d.all(IsMobile) "True"use_backend mobile_site if is_mobiledefault_backend desktop_site
51d.single Jump to heading
Use the converter 51d.single
to perform a lookup in the database that returns the values of the specified properties. It takes the User-Agent
header as an input parameter. The function can be passed up to five property names and if a property name can’t be found, it returns the value NoData
.
Syntax:
text
51d.single(<prop>[,<prop>*])
text
51d.single(<prop>[,<prop>*])
In this example, we create an HTTP request header that contains device information:
haproxy
frontend wwwbind :80mode httphttp-request set-header X-DeviceInfo %[req.hdr(user-agent),51d.single(DeviceType,IsMobile,ScreenPixelsHeight,ScreenPixelsWidth)]
haproxy
frontend wwwbind :80mode httphttp-request set-header X-DeviceInfo %[req.hdr(user-agent),51d.single(DeviceType,IsMobile,ScreenPixelsHeight,ScreenPixelsWidth)]
Update the database during runtime Jump to heading
Use the 51Degrees Update module to keep the contents of the device detection database current. This allows you to keep multiple load balancers synced with the latest data.
-
Install a web server of your choice and host the database file at a URL the load balancer can access. For example, host the file at
http://192.168.0.1:8000/51Degrees-LiteV4.1.hash
. -
Install the package
hapee-3.0r1-lb-51d-update
.nixsudo apt-get install hapee-<VERSION>-lb-51d-updatenixsudo apt-get install hapee-<VERSION>-lb-51d-updateExample for HAProxy Enterprise 3.0r1:
nixsudo apt-get install hapee-3.0r1-lb-51d-updatenixsudo apt-get install hapee-3.0r1-lb-51d-updatenixsudo yum install hapee-<VERSION>-lb-51d-updatenixsudo yum install hapee-<VERSION>-lb-51d-updateExample for HAProxy Enterprise 3.0r1:
nixsudo yum install hapee-3.0r1-lb-51d-updatenixsudo yum install hapee-3.0r1-lb-51d-updatenixsudo zypper install hapee-<VERSION>-lb-51d-updatenixsudo zypper install hapee-<VERSION>-lb-51d-updateExample for HAProxy Enterprise 3.0r1:
nixsudo zypper install hapee-3.0r1-lb-51d-updatenixsudo zypper install hapee-3.0r1-lb-51d-updatenixsudo pkg install hapee-<VERSION>-lb-51d-updatenixsudo pkg install hapee-<VERSION>-lb-51d-updateExample for HAProxy Enterprise 3.0r1:
nixsudo pkg install hapee-3.0r1-lb-51d-updatenixsudo pkg install hapee-3.0r1-lb-51d-update -
Add the following lines to the
global
section of your configuration:haproxyglobalmodule-load hapee-lb-51d-update.so51degrees-update url http://192.168.0.1:8000/51Degrees-LiteV4.1.hash delay 24h loghaproxyglobalmodule-load hapee-lb-51d-update.so51degrees-update url http://192.168.0.1:8000/51Degrees-LiteV4.1.hash delay 24h logBe sure to specify the port number where your file is hosted, for example
8000
as in the example above.
With this configuration, HAProxy Enterprise downloads the database every 24 hours and prints a message in the logs when it succeeds or if it encounters errors during the update.
Runtime API Jump to heading
It is possible to manage the 51Degrees module via the Runtime API.
51d-update force-update Jump to heading
Prerequisites
This command becomes available after you have installed both the 51Degrees and 51Degrees Update modules.
The 51Degrees update module will update the database based on the interval you specifed in the configuration. Use the 51d-update force-update
command to force an immediate update of the 51Degrees database.
In this example, we force an update of the database.
nix
echo "51d-update force-update" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock
nix
echo "51d-update force-update" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock
outputtext
51Degrees: forcing update now
outputtext
51Degrees: forcing update now
51d-update status Jump to heading
Prerequisites
This command becomes available after you have installed both the 51Degrees and 51Degrees Update modules.
You can check the status of the update using the 51d-update status
command. Note that while the update is processing, the status will show the progress:
nix
echo "51d-update status" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock
nix
echo "51d-update status" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock
outputtext
51Degrees module status------------------------------------------------------------------------------initialized: yesDatabase updateconfiguration: /etc/hapee-2.7/hapee-lb.cfg:43url: http://192.168.64.1/51Degrees-LiteV4.1.hashhttp status count: 0 0 0 0 7 / 0period/delay: 1d / 5s 10s 5suse cksum/hash/mod: no / no / noreload/retry count: 0 0 2 / 7 2reload time: <NEVER> / <NEVER> / 2023-08-08 14:25:37currently updating: 51Degrees-LiteV4.1.hashstatus/retry: 0 / 1data size: 0 / 0 (0.0%)dur/time left: 1m49s / 3s
outputtext
51Degrees module status------------------------------------------------------------------------------initialized: yesDatabase updateconfiguration: /etc/hapee-2.7/hapee-lb.cfg:43url: http://192.168.64.1/51Degrees-LiteV4.1.hashhttp status count: 0 0 0 0 7 / 0period/delay: 1d / 5s 10s 5suse cksum/hash/mod: no / no / noreload/retry count: 0 0 2 / 7 2reload time: <NEVER> / <NEVER> / 2023-08-08 14:25:37currently updating: 51Degrees-LiteV4.1.hashstatus/retry: 0 / 1data size: 0 / 0 (0.0%)dur/time left: 1m49s / 3s
51d-update show Jump to heading
Prerequisites
This command becomes available after you have installed both the 51Degrees and 51Degrees Update modules.
Use the 51d-update show
command to display the configuration of the 51Degrees update module. The output of this command includes information about each configured database file and its update status.
Below, we retrieve the status information for our configured databases:
nix
echo "51d-update show" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock
nix
echo "51d-update show" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock
outputtext
# 51d-update configuration# url: next updatehttp://192.168.0.1/51Degrees-LiteV4.1.hash: 23h58m
outputtext
# 51d-update configuration# url: next updatehttp://192.168.0.1/51Degrees-LiteV4.1.hash: 23h58m
Troubleshooting Jump to heading
This section covers troubleshooting steps.
Error reading 51Degrees data file Jump to heading
You get the following error during startup:
text
51Degrees Setup - Error reading 51Degrees data file. The data (file: '/etc/hapee-3.0/51Degrees-EnterpriseV3.4.trie') is an unsupported version. Check you have the latest data and API.
text
51Degrees Setup - Error reading 51Degrees data file. The data (file: '/etc/hapee-3.0/51Degrees-EnterpriseV3.4.trie') is an unsupported version. Check you have the latest data and API.
The data file you are trying to load is not compatible with the version of the HAProxy Enterprise 51Degrees module. For example, you tried to load a .trie
data file, but the module expects a .hash
data file.
- For HAProxy Enterprise 3.0r1 and newer, use the V4
.hash
data file. - For HAProxy Enterprise 2.9r1 and older, use the V3
.trie
data file.
Do you have any suggestions on how we can improve the content of this page?