Nginx map instruction Matching User Agent Custom Value Example

Source: Internet
Author: User
Tags curl log log php file regular expression user agent switcher

This article describes knowledge about user-agent and the use of the Nginx Map directive to match the user Agent custom values with regular expressions, and there are many things you can do by capturing UA custom values, one of which is to have a test machine that supports multiple tests while testing a project. The principle is to match the UA value and set a different WEB root directory.

about the User Agent

The user agent in Chinese is called "UA," which is a special string header that enables the server to identify the operating system and version, CPU type, browser and version, browser rendering engine, browser language, browser plugin, and so on that the client uses.

With the Firefox Firebug tool, you can see that the request header information for each request contains a user-agent field, as shown in the figure:

By viewing the request log of nginx such as Access.log, you can also view the UA information for each request, and if it is Curl command request, you can see that UA information is curl:––[10/oct/2016:02:42:59 +0000] "Get/hello http/1.1"-"" mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) gecko/20100101 firefox/48.0 Tanteng "––[10/oct/2016:02:43:23 +0000] "Get/hello http/1.1"-"curl/7.35.0"

Firefox plugin: User Agent switcher

This plugin can manage and switch different user-agent, such as simulating different browsers, or adding custom UA, which can bring its own identity in UA.

For example, add your own UA, and add your own logo, as shown:

In this way, the UA in the request header information of Firefox is the modified one, access to a local WEB project, through Firebug or view the Access.log log of Nginx, can find the request UA changed.

Nginx Configuration Matching User-agent

In the Nginx configuration file, $http _user_agent represents the value of UA, such as the browser default UA value:

mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) gecko/20100101 firefox/48.0

Now a new Ua,ua value is added with a space and a name in the back, such as:

mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) gecko/20100101 firefox/48.0 Tanteng

Through the Firefox User Agent swicher This plugin to the new UA, and then through the browser access, UA is this modified.

So, how do you match this last name in Nginx? Here you can use the Nginx map instruction to match the regular expression.

Map instruction Regular Match

The map instruction is provided using the Ngx_http_map_module module, and the Ngx_http_map_module module can create variables that are associated with the values of other variables. Allowing classification or mapping of multiple values to multiple different values and storing them in a variable, the map instruction is used to create variables, but only when the variable is accepted the view mapping operation is performed, and the module does not have a performance flaw when handling requests that do not have reference variables.
In the Nginx HTTP domain, add the following code:

Map $http _user_agent $user {
Default ';
~curl Curl;
~ (? <name>[\s]+$) $name;

Here the regular expression [\s]+$ matches the last non-white-space character,?<name> indicates that the result to match is expressed in $name.

The map field means that the matching object is $http _user_agent, UA, the default $user value is an empty string, and if the curl begins, the $user value is curl, and if it can be matched to the last Non-empty string, then the $user value is the last character String.

Add the following test code to the server domain:

Location/hello {
echo http_user_agent: $http _user_agent;
echo User: $user;

Execute Curl Command: Curl

return Result:


Accessed in the browser, the output results are:

http_user_agent:mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) gecko/20100101 firefox/48.0 Tanteng

This succeeds in obtaining a value that is incremented after UA.

Nginx the site root directory based on different UA

After obtaining UA custom values through the map instruction, you can set the following to allow different testers to correspond to different site roots:

Location ~ \.php$ {
Try_files $uri = 404;
Fastcgi_index index.php;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
Include Fastcgi_params;

Here root row, the site directory can be represented by $user variables. For example, the tester ruike, he modified his UA followed by Ruike, then visit the site root is/usr/share/nginx/html/ruike/public, so that different testers in their own directory pull code, do not affect each other, Support for simultaneous testing.

Test the effect and put the index.php file in front of the public with Echo ' Wo shi ruike. ' Exit Output.

Switch to ruike this UA, visit the website, show that is the above content, switch back to Tanteng this UA, showing the normal content of the site, which reached through the UA custom values to set different Web site root effect, but also support a test machine to support different testers to test the needs of the same time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.