Caching performance on NGiNX location folder vs location regex

I see a lot of regex examples for caching files like:

location ~* ^.+\.(?:css|js|jpeg|jpg|gif|ico|png|html|ttf|eot|woff|woff2|svg)$ {
    add_header Cache-Control public,max-age=2592000,immutable;
    access_log off;
    tcp_nodelay off;
    open_file_cache max=3000 inactive=120s;
    open_file_cache_valid 45s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;
}

However from reading about how NGiNX works it looks like this can handle many times more requests?

location /stufftocache/ {
    add_header Cache-Control public,max-age=2592000,immutable;
    access_log off;
    tcp_nodelay off;
    open_file_cache max=3000 inactive=120s;
    open_file_cache_valid 45s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;
}

and just put everything in that folder, is this true or do I not understand it? I cannot find any questions asking this nor any examples stating this, but regex seems to be far slower.


Yes, regular expressions are slower, especially if they are build sub-optimally, which is easy...

For example, your regular expression can be written as:

location ~* \.(?:css|js|jpeg|jpg|gif|ico|png|html|ttf|eot|woff|woff2|svg)$

which makes it a bit faster by skipping the start of line matching.

About the actual question, yes, prefix matching is faster.

However, it isn't often recommended because it limits where one can put the files, therefore making it harder to configure and keep configuration up-to-date when new files are added to the system.