NGINX: map and try_files not working

Solution 1:

The try_files statement is using numeric captures from the regular expression in the location statement. The problem with numeric captures is that they are overwritten whenever a regular expression is evaluated. The map contains a regular expression that is evaluated between the location and the try_files statements.

The solution is to use named captures:

For example:

location ~ ^/imgs/(?<prefix>[0-9]*)/(.*?)(?<suffix>\..+)$ {
    add_header X-webp $webp_suffix;
    try_files /imgs/$prefix$webp_suffix /imgs/$prefix$suffix =404;
}