Nginx: is there an advantage, real or theoretical, of using "^.+.ext$" instead of ".ext$" in a regular expression location block?

Harold Fischer asked:

Is there any reason, real or theoretical, that:

location ~* ^.+\.js$ {
    access_log off;
    log_not_found off;
    expires max;
}

would be preferable over:

location ~* \.js$ {
    access_log off;
    log_not_found off;
    expires max;
}

I ask because most of the time, I see Nginx examples using the configuration of the second block. But I sometimes also see the first block being used in lieu of the the second. In fact, many examples on nginx.com seem to use the configuration of the first block instead of the second one.

As far I can tell, the only real difference is that the first block omits searches for newlines (\n) when matching locations, possibly making it more efficient.

Readability is nice, but performance is nicer.

Can anyone make an argument for choosing one configuration over the other, seeing as they would seem to be almost identical in terms of function?

My answer:


The first regex takes slightly longer to execute, but on modern CPUs the time is going to be pretty trivial.

But, the first regex will not match a file named .js. The second regex will match a file named .js.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.