Nginx bug when mixing map and try_files ?

Francis Daly francis at daoine.org
Wed Oct 7 18:08:36 UTC 2020


On Wed, Oct 07, 2020 at 06:50:30PM +0200, Marcin Wanat wrote:

Hi there,

> i am doing simple webp client support check using map and then using
> try_files to check if file exists and serve it.

$1 may not mean what you want it to mean, when more than one regex-thing
is involved. And "map" can be a regex-thing.

>                         location ~ ^/imgs/([0-9]*)/(.*)$ {
>                                         add_header X-webp $webp_suffix;
>                                         try_files /imgs/$1$webp_suffix
> /imgs/$1.jpg =404;
>                         }

If you change the location regex to whatever your engine's version of
"save this in a named variable" is, then use that named variable in the
try_files line, you may have better luck.

Perhaps:

  location ~ ^/imgs/(?P<numbers>[0-9]*)/(.*)$ {

and use $numbers instead of $1.

> What am i missing ?
> 
> 1) Why it does not serve .webp file at all ?
> 2) Why when try_files has webp check on first position it DO NOT serve .jpg
> file but 404 and when i swap order of parameters i DO serve .jpg ?

When nginx needs to know the value of $webp_suffix, the "map" is used,
which messes with your $1.

Then when nginx substitutes in the value of $1, it is not what it was
from the regex location.

So it all Just Works when $webp_suffix does not need to be read.

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list