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
-
Log into your account at the 51Degrees website and download the enterprise database in Trie format. Copy the
.trie
file to your HAProxy Enterprise server (for example/etc/hapee-2.9/51Degrees-LiteV3.4.trie
). -
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 2.9r1:
nixsudo apt-get install hapee-2.9r1-lb-51dnixsudo apt-get install hapee-2.9r1-lb-51dnixsudo yum install hapee-<VERSION>-lb-51dnixsudo yum install hapee-<VERSION>-lb-51dExample for HAProxy Enterprise 2.9r1:
nixsudo yum install hapee-2.9r1-lb-51dnixsudo yum install hapee-2.9r1-lb-51dnixsudo zypper install hapee-<VERSION>-lb-51dnixsudo zypper install hapee-<VERSION>-lb-51dExample for HAProxy Enterprise 2.9r1:
nixsudo zypper install hapee-2.9r1-lb-51dnixsudo zypper install hapee-2.9r1-lb-51dnixsudo pkg install hapee-<VERSION>-lb-51dnixsudo pkg install hapee-<VERSION>-lb-51dExample for HAProxy Enterprise 2.9r1:
nixsudo pkg install hapee-2.9r1-lb-51dnixsudo pkg install hapee-2.9r1-lb-51d -
In the
global
section of your configuration, add the following lines. Change the51degrees-property-name-list
depending on the properties you want to use:haproxyglobalmodule-path /opt/hapee-2.9/modules/module-load hapee-lb-51d.so51degrees-data-file /etc/hapee-2.9/51Degrees-LiteV3.4.trie51degrees-property-name-list DeviceType IsMobile IsTablethaproxyglobalmodule-path /opt/hapee-2.9/modules/module-load hapee-lb-51d.so51degrees-data-file /etc/hapee-2.9/51Degrees-LiteV3.4.trie51degrees-property-name-list DeviceType IsMobile IsTablet -
Reload the configuration to apply the changes.
nixsudo systemctl reload hapee-2.9-lbnixsudo systemctl reload hapee-2.9-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,IsTablet)]
haproxy
frontend wwwbind :80mode httphttp-request set-header X-DeviceInfo %[51d.all(DeviceType,IsMobile,IsTablet)]
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,IsTablet)]
haproxy
frontend wwwbind :80mode httphttp-request set-header X-DeviceInfo %[req.hdr(user-agent),51d.single(DeviceType,IsMobile,IsTablet)]
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-LiteV3.4.trie
. -
Install the package
hapee-2.9r1-lb-51d-update
.nixsudo apt-get install hapee-<VERSION>-lb-51d-updatenixsudo apt-get install hapee-<VERSION>-lb-51d-updateExample for HAProxy Enterprise 2.9r1:
nixsudo apt-get install hapee-2.9r1-lb-51d-updatenixsudo apt-get install hapee-2.9r1-lb-51d-updatenixsudo yum install hapee-<VERSION>-lb-51d-updatenixsudo yum install hapee-<VERSION>-lb-51d-updateExample for HAProxy Enterprise 2.9r1:
nixsudo yum install hapee-2.9r1-lb-51d-updatenixsudo yum install hapee-2.9r1-lb-51d-updatenixsudo zypper install hapee-<VERSION>-lb-51d-updatenixsudo zypper install hapee-<VERSION>-lb-51d-updateExample for HAProxy Enterprise 2.9r1:
nixsudo zypper install hapee-2.9r1-lb-51d-updatenixsudo zypper install hapee-2.9r1-lb-51d-updatenixsudo pkg install hapee-<VERSION>-lb-51d-updatenixsudo pkg install hapee-<VERSION>-lb-51d-updateExample for HAProxy Enterprise 2.9r1:
nixsudo pkg install hapee-2.9r1-lb-51d-updatenixsudo pkg install hapee-2.9r1-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-LiteV3.4.trie delay 24h loghaproxyglobalmodule-load hapee-lb-51d-update.so51degrees-update url http://192.168.0.1:8000/51Degrees-LiteV3.4.trie 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-2.9/hapee-lb.sock
nix
echo "51d-update force-update" | sudo socat stdio unix-connect:/var/run/hapee-2.9/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-2.9/hapee-lb.sock
nix
echo "51d-update status" | sudo socat stdio unix-connect:/var/run/hapee-2.9/hapee-lb.sock
outputtext
51Degrees module status------------------------------------------------------------------------------initialized: yesDatabase updateconfiguration: /etc/hapee-2.7/hapee-lb.cfg:43url: http://192.168.64.1/51Degrees-LiteV3.4.triehttp 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-LiteV3.4.triestatus/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-LiteV3.4.triehttp 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-LiteV3.4.triestatus/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-2.9/hapee-lb.sock
nix
echo "51d-update show" | sudo socat stdio unix-connect:/var/run/hapee-2.9/hapee-lb.sock
outputtext
# 51d-update configuration# url: next updatehttp://192.168.0.1/51Degrees-LiteV3.4.trie: 23h58m
outputtext
# 51d-update configuration# url: next updatehttp://192.168.0.1/51Degrees-LiteV3.4.trie: 23h58m
Do you have any suggestions on how we can improve the content of this page?