Are multiple location blocks faster than multiple regex rewrites in a single block?
Solution 1:
Most likely the difference in performance is quite minimal, and you would require a long test run for each case to see which one performs the best.
Here are some alternatives how to implement your requirements:
No capture in statements
location / {
rewrite ^/foo/ /index.php last;
rewrite ^/bar/ /index.php last;
rewrite ^/baz/ /index.php last;
}
There is no need to use the capture group in the regex, since the value isn't used for anything.
One regular expression to match all possibilities
location / {
rewrite ^/(?:foo|bar|baz) /index.php last;
}
This regex combines the three alternatives into a single expression. The ?:
prevents the regular expression capture, which would otherwise happen when parentheses are used.
Match all parts in regular expression in location
.
location ~ ^/(?:foo|bar|baz) {
rewrite ^ /index.php last;
}
Ignore regular expression match with location prefix match
location ^~ /foo/ {
rewrite ^ /index.php last;
}
location ^~ /bar/ {
rewrite ^ /index.php last;
}
location ^~ /baz/ {
rewrite ^ /index.php last;
}
location / {
try_files $uri $uri/ /index.php;
}
You should measure the performance between these options to see which is fastest. The speed might also be affected by other things in the configuration.
These alternatives do work a bit different with respect to other configuration, so you need to make sure that other parts of the app work correctly.