Coder Social home page Coder Social logo

device-detection-nginx's Introduction

51Degrees Device Detection Nginx Module

51Degrees Device Detection in C Nginx module

Developer documentation

Introduction

This project integrates 51Degrees Device Detection V4 engine as a module to Nginx, allowing users to configure Nginx to perform device detection by setting corresponding 51Degrees directives in the Nginx configuration file. Currently only Linux platform is supported.

Getting Started

Before following the quick start, make sure to have Nginx installed and have a 51Degrees data file ready to use. The Nginx executable can be installed and built as instructed in the Build and Test section.

Data File

In order to perform device detection, you will need to use a 51Degrees data file. This repository includes a free, 'lite' file in the 'device-detection-data' sub-module that has a significantly reduced set of properties. To obtain a file with a more complete set of device properties see the 51Degrees website. If you want to use the lite file, you will need to install GitLFS.

For Linux:

sudo apt-get install git-lfs
git lfs install

Then, navigate to the device-detection-cxx/device-detection-data directory and execute:

git lfs pull

Quick Start

To quickly start using 51Degrees module with Nginx, please follow the below steps:

  • Install an Nginx version 1.19.0 or above.
  • Obtain a 51Degrees data file. A Lite version is available at Github.
  • Download the pre-built binaries at our Github release page.
  • Choose the binary for the version of Nginx that you want to use.
  • Create a folder which we will use for this quick start.
  • In the folder:
    • Create a Nginx configuration file and paste the following content, replacing the path in load_module and 51D_file_path with the actual path to the downloaded module and the downloaded data file.
# Specify the module to load. 
load_module [path to 51Degrees module];

events {
}

http {
    # Specify the data file to use
    51D_file_path [path to 51Degrees data file];
    server {
        listen 8888;

        location /test {
            # Perform a detection using a single User-Agent header.
            # Set the request header 'x-mobile' to returned value for property 'IsMobile'.
            51D_match_ua x-mobile IsMobile;

            # Set the response header 'x-mobile' using the request header 'x-mobile'.
            add_header x-mobile $http_x_mobile;
        }
    }
}
  • Create a folder named html and an empty file test in that folder. This is because we are serving static content in the above config file and the root directory to serve requests is defaulted at html.
  • Create a folder named logs for nginx to write its log messages to.
  • In the quick start folder, start Nginx using the following command:
nginx -p . -c nginx.conf
- The `-p` flag tells Nginx to set the prefix path which Nginx will use.
- The `-c` flag tells Nginx to use the config file that we just created.
  • Install curl if you haven't.
  • Fire a request and see the result being sent back in the response using the following command:
curl -H 'User-Agent: 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' http://localhost:8888/test -I
  • You should see the x-mobile: True in the response header, indicating that Nginx has successfully used 51Degrees Device Detection engine to detect that client user agent is a mobile device using the input 'User-Agent' header.
  • At this point you can terminate the Nginx server using:
nginx -p . -s quit

The above gave you a quick overview of how to use Nginx 51Degrees Device Detection V4 module. However, that used a pre-built binaries of 51Degrees module, and in some cases, it might not work on your target machine due to the differences in the build environment that was used. In those cases, you will have options to rebuild the module yourself. This is detailed in the Build and Test section.

Latest releases

The latest releases of 51Degrees Nginx module can be found on Github.

For the supported platforms and Nginx version, please check the tested versions page for latest update. At the time of writing, the followings are supported:

  • Platform Ubuntu-18.04 and 20.04, 64bit.
  • Nginx version: 1.19.0, 1.19.5, 1.19.10 and 1.20.0

API references

Below is the list of directives that can be used with 51Degrees Device Detection V4 module.

Directives
Syntax: 51D_file_path filename;
Default: ---
Context: main
Specify the data file to used for 51Degrees Device Detection V4 engine
Syntax: 51D_drift drift;
Default: 51D_drift 0;
Context: main
Specify the drift value that a detection can allow.
Syntax: 51D_difference difference;
Default: 51D_difference 0;
Context: main
Specify the difference value that a detection can allow.
Syntax: 51D_allow_unmatched on | off;
Default: 51D_allow_unmatched off;
Context: main
Specify if unmatched should be allowed.
DEPRECATED Syntax: 51D_use_performance_graph on | off;
Default: 51D_use_performance_graph off;
Context: main
Specify if performance graph should be used in detection. DEPRECATED: Has no effect on configuration, the data file has a single (predictive) graph that is always used.
DEPRECATED Syntax: 51D_use_predictive_graph on | off;
Default: 51D_use_predictive_graph on;
Context: main
Specify if predictive graph should be used in detection. DEPRECATED: Has no effect on configuration, the data file has a single graph that is always used.
Syntax: 51D_value_separator separator;
Default: 51D_value_separator ',';
Context: main
Specify the separator to be used in the value string returned from a detection. Each value in the returned result string is correspond to a requested property.
Syntax: 51D_match_ua header properties [argument];
Default: ---
Context: main, server, location (NOTE: This directive can be used in main, server and location blocks. Specified properties are aggregated and eventually queried in the location. header value is set after the query is performed and is only available within location block)
Perform a detection using a single request header User-Agent. header specifies which request header the returned properties values should be stored at. properties is a comma separated list string. argument specifies if a User-Agent is supplied as a query argument. This will override the value in the User-Agent header. The argument is optional.
If a property is not available for any reason, the value being returned for that property will be NA
This directive was previously known as 51D_match_single (name deprecated)
Syntax: 51D_match_ua_client_hints header properties [argument];
Default: ---
Context: main, server, location (NOTE: This directive can be used in main, server and location blocks. Specified properties are aggregated and eventually queried in the location. header value is set after the query is performed and is only available within location block)
Perform a detection using request headers User-Agent and Sec-CH-UA-*. header specifies which request header the returned properties values should be stored at. properties is a comma separated list string. argument specifies if a User-Agent is supplied as a query argument. This will override the value in the User-Agent header. The argument is optional.
If a property is not available for any reason, the value being returned for that property will be NA
Syntax: 51D_match_all header properties;
Default: ---
Context: main, server, location (NOTE: This directive can be used in main, server and location blocks. Specified properties are aggregated and eventually queried in the location. header value is set after the query is performed and is only available within location block)
Perform a detection using all headers, query argument and cookie from a http request. header specifies which request header the returned properties values should be stored at. properties is a comma separated list string.
If a property is not available for any reason, the value being returned for that property will be NA
Syntax: 51D_get_javascript_single javascript_property [argument];
Default: ---
Context: location
Perform a detection using a single request header User-Agent. The returned value of javascript_property is set in the response body. This works in a similar way as CDN to serve static content. argument specifies if a User-Agent is supplied as a query argument. This will override the value in the User-Agent header. The argument is optional.
If the Javascript property is not available for any reason, a Javascript block comment will be returned so that it will not cause syntax error when the client executes it.
The whole response body is used for the returned content so only one of these directives can be used in a single location block. Also, since the static content does not actually exist as a static file, the nginx http core module will log an error, so it is recommended to use this directive with log_not_found set to off.
Syntax: 51D_get_javascript_all javascript_property;
Default: ---
Context: location
Perform a detection using all headers, cookie and query arguments from a http request. The returned value of the javascript_property is set in the response body. This works in a similar way as CDN to serve static content.
If the Javascript property is not available for any reason, a Javascript block comment will be returned so that it will not cause syntax error when the client executes it.
The whole response body is used for the returned content so only one of these directives can be used in a single location block. Also, since the static content does not actually exist as a static file, the nginx http core module will log an error, so it is recommended to use this directive with log_not_found set to off.
Syntax: 51D_set_resp_headers on | off;
Default: 51D_set_resp_headers off
Context: main, server, location
Allow Client Hints to be set in response headers where it is applicable to the user agent (e.g. Chrome 89 or above) so that more evidence can be returned in subsequent requests, allowing more accurate detection. Value set in a block overwrites values set in precedent blocks (e.g. value set in location block will overwrite value set in server and main blocks). This will only be available from the 4.3.0 version onwards.

Proxy Passing

When using the proxy_pass directive in a location block where a match directive is used, the properties selected are passed as additional HTTP headers with the name specified in the first argument of 51D_match_ua/51D_match_ua_client_hints/51D_match_all.

Fast-CGI

Using include fastcgi_params; makes these additional headers available via the $_SERVER variable.

Match Metrics

Other than detection properties, users can request further metrics of a match in the same way. The available metrics are Drift, Difference, Method, MatchedNodes, User-Agents and DeviceId. e.g.

51D_match_all x-metrics Drift,Difference,Method,MatchedNodes,DeviceId,UserAgents

Output Format

The value of the header is set to a comma separated list of values (comma delimited is the default behaviour, but the delimiter can be set explicitly with 51D_value_separator), these are in the same order the properties are listed in the config file. So setting a header with the line:

51D_match_all x-device HardwareName,BrowserName,PlatformName;

will give a header named x-device with a value like Desktop,Firefox,Ubuntu. Alternatively, headers can be set individually like:

51D_match_all x-hardware HardwareName;
51D_match_all x-browser BrowserName;
51D_match_all x-platform PlatformName;

giving three separate headers.

Examples

All examples are located in the examples folder:

Example Description
gettingStarted.conf Shows a simple instance of how to use 51D_match_ua, 51D_match_ua_client_hints and 51D_match_all in a configuration file.
config.conf Shows how to configure 51Degrees detection using directives such as 51D_drift, 51D_difference, etc...
matchQuery.conf Shows how to perform detection using input from http request query argument
matchMetrics.conf Shows how to obtain other match metrics of the detection such as drift, difference, method and etc...
responseHeaders Shows how to enable Client Hints support to request further evidence from user agent to provide more accurate detection. This will only be available from the 4.3.0 version onwards.
jsExample Shows how to use 51D_get_javascript* directive to get the Javascript to obtain further evidences from the client. To run this example, start Nginx with the included javascript.conf set as the configuration file. Once Nginx fully start, open index.html in a browser to see that the screen width is now set correctly.

To run these examples, follow the instructions detailed in each example.

Build and Test (Linux)

Before build and test the 51Degrees Device Detection V4 module, make sure to check out the project and all of its sub-modules.

Fetching sub-modules

This repository has sub-modules that must be fetched. If cloning for the first time, use:

git clone --recurse-submodules https://github.com/51Degrees/device-detection-nginx.git

If you have already cloned the repository and want to fetch the sub modules, use:

git submodule update --init --recursive

If you have downloaded this repository as a zip file then these sub modules need to be downloaded separately as GitHub does not include them in the archive. In particular, note that the zip download will contain links to LFS content, rather than the files themselves. As such, these need to be downloaded individually.

Build

To build 51Degrees Device Detection V4 module, the following libraries are required.

  • C compiler that support C11 or above.
  • make
  • zlib1g-dev
  • libpcre3
  • libpcre3-dev
  • libatomic

To build the module only, run the following command. This will output to ngx_http_51D_module.so in build/modules directory.

make module

To build the module and Nginx, run the following command.

make install

By default this will use the latest version of Nginx specified in the Makefile. To use a specific version of Nginx, run the above command with FIFTYONEDEGREES_NGINX_VERSION variable. e.g.

make install FIFTYONEDEGREES_NGINX_VERSION=[Version]

To build and link the module statically with Nginx, use STATIC_BUILD variable. e.g.

make install STATIC_BUILD=1

The Nginx executable will be output to the project root directory. The prefix path of the Nginx will be set to build directory. A nginx.conf is also included with a simple example of 51D_match_ua, 51D_match_ua_client_hints and 51D_match_all directives, set in a static content location. The build process has created the static file in the build/html folder so you can start Nginx and start sending requests to that static content endpoint.

Test

All tests are allocated in the tests folder.

To be able to test the 51Degrees module the following libraries are required.

  • Perl 5 and prove Test::Harness
  • CMake 10 or above
  • node and jest
  • curl
  • grep

Before running any test, make sure to build the project with make install command. The tests are designed to test the dynamic build only so STATIC_BUILD will not work.

To run the 51Degrees tests with the default 51Degrees Lite data file included in the device-detection-cxx\device-detection-data directory, run the following command:

make test

To run the 51Degrees together with the Nginx test suite, run the following command:

make test-full

These do not run all the required tests as some tests requires properties that are not supported with the Lite version of the data file. To run all tests, obtain a data file with support properties JavascriptHardwareProfile,ScreenPixelsWidthJavascript and ScreenPixelsWidth. Then, use the FIFTYONEDEGREES_DATAFILE variable to specify the file name to run the tests with. The new data file should be placed in the device-detection-cxx\device-detection-data folder and should have different name to 51Degrees-LiteV4.1.hash. If tests still fail, obtain data file with any other properties used in the tests. Below is an example of running tests with different data file:

make [test|test-full] FIFTYONEDEGREES_DATAFILE=51Degrees-EnterpriseV4.1.hash

Performance Test

The project also include a performance test suite. This required CMake 10 or above to be installed.

To run the performance test, please follow the below steps:

  • Make sure to build the project with make install.
  • Navigate to the tests/performance directory.
  • Create a build folder and navigate to it.
  • Run:
cmake ..
cmake --build .
./runPerf.sh
  • This step will build our internal version of Apache Benchmark and use it for performance testing.

For Developer

Build Options

Run the following to build all current supported versions in modules folder:

make all-versions

Test Options

There are number of test options that are recommended for development only.

Build Option Description
FIFTYONEDEGREES_FORMATTER This is only available when running with test or test-full target. This allow to specify the formatter that can be use to format the test report. e.g. make test FIFTYONEDEGREES_FORMATTER=--formatter TAP::Formatter::Junit. This example will output a JUnit report and requires the module TAP::Formatter::JUnit to be installed. cpan and cpanm are recommended to install this module.
FIFTYONEDEGREES_TEST_OUTPUT This specifies the file that the test report should be written to. e.g. make test FIFTYONEDEGREES_TEST_OUTPUT=test.xml

Memory Check

The project can be built with address sanitizer enabled. To do so, run:

make mem-check

The build options mentioned in the Build section are also applicable to this mem-check rule. However, it has been observed that, in some version of Nginx such 1.19.0 or 1.20.0, the address sanitizer will report memory leak in Nginx without 51Degrees module loaded. Thus, when develop project, make sure that the error is confirmed to not be generated from the 51Degrees module.

Stress Test

The project also include a stress test. This test is inherited from V3 version of 51Degrees project. This test can be run to compared the performance against V3 version and to see how the 51Degrees module perform under heavy load. Note that, in V4 we have constructed a slightly different approach to performance testing so the output from this test is only used as a reference to compare with V3 version. For actually performance test, please follow the instructions in Performance Test section.

To run the stress test, Make sure that the project is built with make install and the Apache Benchmark is built as described in the Performance Test section, without running runPerf.sh script. Then, run the test.sh script.

./test.sh

Examples Test

Before running the tests for the example, make sure to obtain a data file that contains all required properties in the examples.

Run the example tests with the obtained data file:

make test-examples FIFTYONEDEGREES_DATAFILE=[Obtained data file]

Javascript and overrides example test

The Javascript and overrides example test is designed to be run separately with Selenium using NodeJs. The test is located in tests/examples/jsExample directory. To run the test, follow the below steps:

  • Make sure to have Chrome, Firefox and Edge installed.
  • Download and install their drivers.
  • Ensure the browser versions and the drivers versions are aligned.
  • Navigate to the directory tests/examples/jsExample and run:
npm install
  • This will installed all required package including the Selenium web driver.
  • Start Nginx with the javascript.conf as described in the file itself.
  • Run the test
npm test
  • Result is output to a file called cdn_test_results.xml.
  • Stop the Nginx.

Re-certify 51Degrees Device Detection module

When a new release is made to 51Degrees Device Detection module, module maintainer will need to re-certify it the Nginx Plus for target releases.

To re-certify, follow the "Partner Build, Functional Testing, and Self Certification Process" in the documentation here then submit your results to Nginx to be reviewed by their engineering team.

Make sure to use the latest versions of NGINX OSS & Plus for re-certification.

A NGINX Plus dev license might be required for testing.

device-detection-nginx's People

Contributors

arturadam avatar automation51d avatar ben51degrees avatar drasmart avatar justadreamer avatar jwrosewell avatar kirstin51d avatar pi-alexander-popel avatar steve51d avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

device-detection-nginx's Issues

gcc+asan build bug: infinite AddressSanitizer: DEADLYSIGNAL inifinite loop

Background

Nightly PRs to Main fails for MemCheck configurations after ubuntu-latest image update.

image

Quick googling reveals a similar problem: https://stackoverflow.com/questions/77672217/gcc-fsanitize-address-results-in-an-endless-loop-on-program-that-does-nothing pointing at address sanitizer bits randomization issue (perhaps the value of the number of randomized bits has bumped to 32 in the OS image, while asan only supports 28) and a solution

Proposed Fix

Please implement the solution from that SO ticket.

nginx device detection not working when it comes to Client Hints only

In nginx module version 4.4.4 with cxx 4.4.11 and common_cxx 4.4.7
when submitting the following CH headers, the results are correctly returned by the 51Degrees UA tester page:

-H 'Sec-CH-UA: "Chromium";v="109", "Not_A Brand";v="99"'
-H 'Sec-CH-UA-Mobile: ?0'
-H 'Sec-CH-UA-Platform: "Linux"'

https://51degrees.com/developers/user-agent-tester?headers=sec-ch-ua%3A+%22Chromium%22%3Bv%3D%22109%22%2C+%22Not_A+Brand%22%3Bv%3D%2299%22%0D%0Asec-ch-ua-mobile%3A+%3F0%0D%0Asec-ch-ua-platform%3A+%22Linux%22

Results:

Property Value
HardwareVendor Unknown
HardwareModel Unknown
HardwareName Desktop, Emulator
PlatformVendor Unknown
PlatformName Linux
PlatformVersion Unknown
BrowserVendor Unknown
BrowserName Chromium Project
BrowserVersion 109
HardwareFamily Emulator
OEM Unknown

But when using the same three headers for device detection using the nginx module , the results are a no match for everything.

Openresty 1.19.9.1 compatible with ngx_http_51D_module

Is it posible that openresty 1.19.9.1 is not compatible with ngx_http_51D_module

I was building module like this:

make module FIFTYONEDEGREES_NGINX_VERSION=1.19.9
nginx: [emerg] module "../ngx_http_51D_module.so" is not binary compatible in /etc/nginx/nginx.conf:4

Segmentation fault with 4.3.0

I have managed to get the module version 4.3.0 built with nginx 1.20.1 using device-detection-cxx 4.2.3 with this patch and common-cxx 4.2.3 with this patch but now on my first attempt to check nginx configuration, I get a segmentation fault. Here is the backtrace:

(gdb) bt
#0  setupResource (manager=manager@entry=0x7fffa18b4a80, resource=resource@entry=0x55b4d59b1bf0, resourceHandle=resourceHandle@entry=0x55b4d59b3c28, 
    freeResource=freeResource@entry=0x7f66b7925b20 <freeDataSet>) at device-detection-nginx-4.3.0/51Degrees_module/src/common-cxx/resource.c:143
#1  0x00007f66b7930661 in fiftyoneDegreesResourceManagerInit (manager=manager@entry=0x7fffa18b4a80, resource=resource@entry=0x55b4d59b1bf0, resourceHandle=resourceHandle@entry=0x55b4d59b3c28, 
    freeResource=freeResource@entry=0x7f66b7925b20 <freeDataSet>) at device-detection-nginx-4.3.0/51Degrees_module/src/common-cxx/resource.c:176
#2  0x00007f66b7927e9a in fiftyoneDegreesHashInitManagerFromFile (manager=manager@entry=0x7fffa18b4a80, config=config@entry=0x7fffa18b4b20, properties=properties@entry=0x7fffa18b4b00, 
    fileName=fileName@entry=0x55b4d577e672 "/usr/local/share/dbfiles/51Degrees/Enterprise-HashV41.hash", exception=exception@entry=0x7fffa18b4ae0)
    at device-detection-nginx-4.3.0/51Degrees_module/src/hash/hash.c:1858
#3  0x00007f66b7927f4b in fiftyoneDegreesHashSizeManagerFromFile (config=config@entry=0x7fffa18b4b20, properties=properties@entry=0x7fffa18b4b00, 
    fileName=0x55b4d577e672 "/usr/local/share/dbfiles/51Degrees/Enterprise-HashV41.hash", exception=exception@entry=0x7fffa18b4ae0)
    at device-detection-nginx-4.3.0/51Degrees_module/src/hash/hash.c:1884
#4  0x00007f66b7922155 in ngx_http_51D_post_conf (cf=0x7fffa18b5030) at device-detection-nginx-4.3.0/51Degrees_module/ngx_http_51D_module.c:534
#5  0x000055b4d4208a58 in ngx_http_block (cf=0x7fffa18b5030, cmd=<optimized out>, conf=<optimized out>) at src/http/ngx_http.c:308
#6  0x000055b4d41e177c in ngx_conf_handler (last=1, cf=0x7fffa18b5030) at src/core/ngx_conf_file.c:463
#7  ngx_conf_parse (cf=cf@entry=0x7fffa18b5030, filename=filename@entry=0x55b4d574be28) at src/core/ngx_conf_file.c:319
#8  0x000055b4d41de975 in ngx_init_cycle (old_cycle=old_cycle@entry=0x7fffa18b5200) at src/core/ngx_cycle.c:284
#9  0x000055b4d41cae69 in main (argc=<optimized out>, argv=<optimized out>) at src/core/nginx.c:292

Any idea about what I could be doing wrong? I have updated 51D_filePath to 51D_file_path in the configuration, used the most recent v4.1 hash file and compiled the module with -DFIFTYONEDEGREES_HASH instead of TRIE previously.

The changes we made to go from v3 to v4 of the module can be found here: exogroup/rpms@4724569#diff-fff1ff83da29010a72d2fba323a3a1c7fc70998683f2dcb2bbd414298b30304b

make[2]: *** [objs/Makefile:1246: objs/ngx_http_51D_module.so] Error 1

Hi,

I have nginx 1.20.1 and trying to install and compile your module for device detection and i got this message after run "make install":

make[2]: *** [objs/Makefile:1246: objs/ngx_http_51D_module.so] Error 1
make[2]: Leaving directory '/device-detection-nginx/vendor/nginx-1.20.1'
make[1]: *** [Makefile:13: install] Error 2
make[1]: Leaving directory '/device-detection-nginx/vendor/nginx-1.20.1'
make: *** [Makefile:86: install] Error 2

PCRE 2.8 leak

When running memory leak tests, a leak is detected from the PCRE 2.8 library.

This is from an allocation for a RegEx when an if statement is present in the config file.

As it is not from this module, detection of leaks from the PCRE library have been suppressed. See suppressions.txt. If this is fixed, the suppression can be removed.

Stack trace from the leak:

Direct leak of 72 byte(s) in 1 object(s) allocated from:
    #0 0x7fa05bf73867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x5589bb29ee17 in ngx_alloc src/os/unix/ngx_alloc.c:22
    #2 0x5589bb2bf635 in ngx_regex_malloc src/core/ngx_regex.c:508
    #3 0x7fa05bd485d2  (/lib/x86_64-linux-gnu/libpcre2-8.so.0+0x115d2)
    #4 0x5589bb329dff in ngx_http_regex_compile src/http/ngx_http_variables.c:2553
    #5 0x5589bb3f1683 in ngx_http_rewrite_if_condition src/http/modules/ngx_http_rewrite_module.c:768
    #6 0x5589bb3f00a5 in ngx_http_rewrite_if src/http/modules/ngx_http_rewrite_module.c:589
    #7 0x5589bb253820 in ngx_conf_handler src/core/ngx_conf_file.c:463
    #8 0x5589bb252831 in ngx_conf_parse src/core/ngx_conf_file.c:319
    #9 0x5589bb2dd53d in ngx_http_core_location src/http/ngx_http_core_module.c:3224
    #10 0x5589bb253820 in ngx_conf_handler src/core/ngx_conf_file.c:463
    #11 0x5589bb252831 in ngx_conf_parse src/core/ngx_conf_file.c:319
    #12 0x5589bb2db6b1 in ngx_http_core_server src/http/ngx_http_core_module.c:2981
    #13 0x5589bb253820 in ngx_conf_handler src/core/ngx_conf_file.c:463
    #14 0x5589bb252831 in ngx_conf_parse src/core/ngx_conf_file.c:319
    #15 0x5589bb2c0f9a in ngx_http_block src/http/ngx_http.c:239
    #16 0x5589bb253820 in ngx_conf_handler src/core/ngx_conf_file.c:463
    #17 0x5589bb252831 in ngx_conf_parse src/core/ngx_conf_file.c:319
    #18 0x5589bb248110 in ngx_init_cycle src/core/ngx_cycle.c:284
    #19 0x5589bb1f4e21 in main src/core/nginx.c:292
    #20 0x7fa05bb1ad8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f)

Run if statement on $http_x_mobile is not working

Nginx version: 1.20.0
OS System: Ubuntu 21.10 (impish)

We want to check the user's device, if it Mobile then redirect him to https://m.example.com.
If Desktop then redirect him to https://www.example.com

The code below working as expected:

http {
     51D_file_path /etc/nginx/51Degrees-LiteV4.1.hash;
     server {
         ...
         51D_match_single x-mobile IsMobile;
         add_header x-mobile $http_x_mobile;
          ...
     }
}

I can see the x-mobile in the respone header.

But when I use the $http_x_mobile in if statement it doesn't work.

http {
     51D_file_path /etc/nginx/51Degrees-LiteV4.1.hash;
     server {
         ...
         51D_match_single x-mobile IsMobile;
         if ($http_x_mobile ~ 'True') {
              return 301 https://m.example.com;
          }
          ...
     }
}

51d_match_ua and 51d_match_ua_client_hints directives

Motivation

See discussion in issue #10. 51d_match_single directive seems to be confusing. In an effort to improve the clarify of the naming that would express the intent - we suggest adding 2 directives with slightly different semantics:

  • 51d_match_ua - for matching only User-Agent header
  • 51d_match_ua_uach - for matching User-Agent and User-Agent Client Hints headers (Sec-CH-UA-* headers).

The latter could lead to additional performance benefits for the set of incoming headers will be filtered and matching performance might be improved.

Objectives

  1. 51d_match_single directive should be marked as explicitly deprecated - that is: supported for existing customers, but discouraged from use in the documentation.
  2. 51d_match_ua should be introduced with the behavior of the current 51d_match_single.
  3. 51d_match_ua_uach should be introduced with a behavior that filters and uses only User-Agent and User-Agent Client Hints headers (Sec-CH-UA-* headers according to the wicg spec) for matching.
  4. Retain the existing 51d_match_all description.

cc: @thias - please review the above design, does it match your requirements?

Repository is being littered with useless commits

Checking if we were running the latest version of the module, I saw that there is currently about one new release per day! But those are triggered by some automation, and there seems to be a bug since most of the time there are no real changes, only line ordering differences in the tests. For instance: 1cd91cc

[PATCH] Fix up bug User-Agent variable usage

This patch fix some issues:
Installing a handler at the rewrite phase leads to inoperability together with the rewrite and lua-openresty modules, since in the case of the first defined worker process, control may not reach 51degrees handler at all, in the case of the second one it is forced to be executed by the last one in the chain of handlers(always after 51degrees)

User-Agent usage code from an arbitrary variable has the bug in the code, which lead to the overwriting of the variable and the impossibility of its subsequent search. the volume for allocating variable memory was calculated incorrectly.

The config merge code does not scale well with main/server/location configurations at once, which lead to 100% cpu load, starting with 8krps per nginx worker process
Patch fix - 51degrees.zip

undefined symbol: __atomic_compare_exchange_16

I've just updated from 4.3.0 to 4.4.0 and the module now fails to load with:

nginx: [emerg] dlopen() "/usr/lib64/nginx/modules/ngx_http_51D_module.so" failed (/usr/lib64/nginx/modules/ngx_http_51D_module.so: undefined symbol: __atomic_compare_exchange_16) in /usr/share/nginx/modules/mod-http-51D.conf:1

This is with the same RHEL7 OS and build environment as previously, using -std=gnu11 in CFLAGS (to fix my issues from #1).

Since RHEL7 has a fairly old version of gcc (4.8.5), I have also tried compiling with 9.3.1 from Software Collections, but I got the same undefined symbol.

51Degrees The data was not in the correct format

Hi Team,

I'm using Ubuntu 20.04 and Nginx/1.23.4. I installed Nginx with the 51Degrees module, but I am encountering the following error.
nginx: [error] 51Degrees The data was not in the correct format. Check the data file '/etc/nginx/device-detection-data-4.4.8/51Degrees-LiteV4.1.hash' is uncompressed.

I used the 51Degrees module binary package_ubuntu-20.04_Nginx1.23.4.zip and the 4.4.8.tar.gz file.

Could you help me fix it?

This is my Nginx configuration file:

51D_file_path /etc/nginx/device-detection-data-4.4.8/51Degrees-LiteV4.1.hash;

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

        # Perform a detection using a single User-Agent header.
        # Set the request header 'x-mobile' to returned value for property 'IsMobile'.
        51D_match_ua x-mobile IsMobile;

        # Set the response header 'x-mobile' using the request header 'x-mobile'.
        add_header x-mobile $http_x_mobile;
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.