R
R
RomanSS2017-03-08 19:28:45
Nginx
RomanSS, 2017-03-08 19:28:45

Is there any point in nested locations in nginx for optimization?

Connoisseurs in nginx, tell me please, is there any point in double locations for optimization? Those. first filter by folder and only then use the regular expression

location /static/img/users/ {
                location ~ ^/static/img/users/([0-9]+)x([0-9]+)/\.(jpg|png|gif|jpeg)$ {
                        try_files $uri /index.php?p1=$1&p2=$2&p3=$3;
                }
 }

Or there will be no difference in performance if you immediately use this option with one location
location ~ ^/static/img/users/([0-9]+)x([0-9]+)/\.(jpg|png|gif|jpeg)$ {
               try_files $uri /index.php?p1=$1&p2=$2&p3=$3;
}

Both options work for me, but I would like to know which is better to use. According to my logic, the first option should be faster, since it will not use the regular expression for all hits.
Maybe there is another spelling?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2017-03-08
@RomanSS

First, non-regex locations are checked,
i.e., prefixes, exact matches (=), and prefixes with no
regular expressions (^~).
Point A. If a location is found and it has nested locations, then the search
moves inwards. Inside, first locations are checked that are
not specified by regular expressions, if found and it has nested ones,
then go to point A. If not found, then locations
with regular expressions are checked. If location is found, then the search stops.
mailman.nginx.org/pipermail/nginx-ru/2011-October/...
It's better not to worry about such optimization, make it easier to read and edit the config.
UPD:
It will also be useful:
How nginx handles requests
Igor Sysoev: Scalable nginx configuration

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question