About Using Regex in NGINX Map

About Using Regex In NGINX Map

The map module of NGINX enables you to compare values of the NGINX variable against several different conditions before letting you link a new value to it according to the match. This makes the map module a more concise and elegant solution.

Furthermore, you can significantly shorten as well as simplify configuration files with the help of the map directive of NGINX, which uses PCRE (Perl Compatible Regular Expressions). In this article, we will discuss how using Regex in NGINX Map is different from using NGINX Maps under usual scenarios.

Using NGINX Maps in Typical Situations

Maps are highly useful and beneficial for NGINX configuration. However, it is common for people to avoid the use of NGINX maps and opt for using only sets for variable creation as they can sometimes prove to be difficult to visualize.

You can rewrite the NGINX map as a bash in order to monitor what is taking place inside of a map. The first prerequisite to gaining an understanding of the method of variable comparison is knowing how to use an NGINX map and understanding what it actually is.

Firstly, NGINX maps do not add any overhead cost as it does not perform a map variable lookup until they are actually needed. They are determined at the block level of http{} and if a particular configuration section that witnesses the usage of a map variable is not touched upon by a request flow, NGINX does not use a variable.

This makes it one of the best, if not the best, ways of low overhead variable creation.

It is important to understand that the map not adding additional costs to request processing is not the only factor that needs to be considered here, but the evaluation of variables taking place only in the case when they are actually used as well.

Using Regex in NGINX Map

As the configuration of NGINX is usually indicative (applying to the map directive as well), you cannot evaluate it until the processing of the request is carried out, even though it is held in the HTTP context.

This means that we specify the formula that evaluates the result upon need as well, in addition to specifying the result of the evaluation upon the use of the resulting variable in if, location, server contexts, etc. This approach may be sophistical, but it never causes any issues until and unless regular expressions with captures that are unnamed are used. 

Let us take a look at an example to gain a better understanding of it. Suppose you have a domain, i.e., abc.com. Then you have several third-level subdomains as well, such as en.abc.com and de.abc.com. When you wish to redirect them to different or new subdomains, like en.abc.org and de.abc.org, etc., you will not have to describe a huge number of redirect lines, but do this instead:

map $host $redirect_host { default “abc.org”; “~^(\S+)\.abc\.com$” $1.abc.org; }

server { listen*:80; server_name .abc.com; location / { rewrite ^(.*)$ https://$redirect_host$1 permanent; } }

Ideally, the Regex will get analyzed in the map when we request ru.abc.com and the u.abc.org value is the one that the $redirect_host variable will have when it reaches the location. However, everything looks different in reality:

$ GET -Sd en.abc.com

GET http://en.abc.com

301 Moved Permanently

GET https://en.abc.orgen

This gives our rewrite a regex inside another regex. The best solution is to not use regex both where the variable is analyzed as well as in the map:

map $host $redirect_host { default “abc.org”; “~^(\S+)\.abc\.com$” $1.abc.org;}

server { listen*:80;  server_name .abc.com; location} / { return 301 https://$redirect_host$request_uri;}

Volatile Warning Regarding Matching With Regular Expressions

To match a pattern with a complex string to source variables, Regex, a regular expression, is highly useful. However, the only downside to using it is that it adds a little overhead for parsing the expression.

As an NGINX map carries out a single lookup by default for each request that is in the process of being processed, the overhead that Regex is responsible for is usually limited to only a single lookup. However, when you enable the volatile, caching on the variable during a request gets turned off by the map parameter after only the first lookup.

This means that each time you use an NGINX map, an additional full lookup gets performed, resulting in increasing the overhead for that particular request. This causes performance degradation as well when the volume is high.

What is more, is that the caching on the variables of a dependent map that needs a lookup to be performed on the map variable of volatile is also turned off with the use of volatile. In the cases of an associated map variable and any other variable that is dependent on that map being indicated in multiple places, it is recommended not to use the volatile; parameter on that specific map if there is a requirement for a complex regular expression.

It is extremely difficult to notice it unless the utilization of the CPU rises and the lookups get amplified due to significant traffic. You should always treat volatile with extreme care as it does not show itself as the factor causing the CPU increase.

The Process Isn’t Simple – But Is Definitely Worth It

Using Regex in the NGINX map is not only quite straightforward and simple but highly useful as well as saves considerable time and effort. The PHP page does all the hard work, making NGINX configuration short and easy. It shows the results by analyzing the values that the user enters and generating the NGINX configuration file that is necessary on the basis of those values. The PHP page then reloads NGINX and sends a request to it.

Conclusion About Using Regex In NGINX Map

There are a number of free regex testers available on the internet, along with all the codes for you to try out. The available regex testers come with all the files that are necessary to easily get it up and running. Once you use a tester for regular expressions, it will give you a good idea of the simplicity, flexibility, and power of NGINX. Contact us for solutions about using Regex in NGINX Map.

Further blogs within this About Using Regex In NGINX Map category.