Most Effective Regular Expression for Nginx Location

What is the most efficient way to determine a location directive that matches something like

location = /[0-9a-zA-Z_-]{1,6} { content_by_lua_file ....} 

In other words, a URI that matches a string of 1 to 6 characters with the digits "-", "_", numbers and letters.

Or it’s faster to check the string length in my LUA code, which will generate the result using the location directive, for example,

 location / {content_by_lua_file...} 
+8
regex webserver nginx
source share
2 answers

Regular expressions are very effective in what they do.

When a task is trivial (e.g. checking for a specific string), a string function can be faster than a regular expression, depending on the platform. Here you check both the range of characters and the length. It is unlikely that Lua code (compiled at runtime) will be faster than the pre-compiled C code of the PCRE regex library used by nginx.

In general, a regular expression for a string from 1 to 6 characters with "-", "_", digits and letters can be written as

 ^[-\w]{1,6}$ 

It's because

  • Anchor ^ states that we are at the beginning of the line
  • The character \w matches letters, numbers, and the underscore
  • The $ anchor states that we are at the end of the line

However, in nginx, the ~ operator (the request begins with) allows us to abandon the initial anchor ^ . You should write something like this:

 location ~ [-\w]{1,6}$ { # some rewrite code, for example # rewrite ^([^/]+)/?$ /oldsite/$1 break; } 

Another piece of information for the curious: in Lua itself, the above regular expression can be turned into a Lua pattern, where % used instead of \ to form metacharacters:

 ^[-%w]{1,6}$ 

Link

+9
source share

I think in Lua you will need to check not only the length, but also the contents of the string.
Nginx uses the C PCRE library for regular expressions.
There is also PCRE-JIT that JIT compiles the regular expression, especially useful if the regular expression is more complex than the one in your question. I think Nginx is faster.

+1
source share

All Articles