Nginx bug when mixing map and try_files ?

Marcin Wanat marcin.wanat at
Wed Oct 7 16:50:30 UTC 2020


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

Nginx 1.18, my complete nginx.conf:
events {
    use           epoll;
    worker_connections  128;

http {

        # Check if client is capable of handling webp
        map $http_accept $webp_suffix {
                   default "";
                   "~*webp" ".webp";

        server {
                        listen      *:8888;
                        server_name   test;
                        root  /srv;

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


Now i am opening:

And it results in error 404.
Files /srv/imgs/10.jpg and /srv/imgs/10.webp do exist.

When i spoof my client and remove webp from http accept list, then
everything works ok and serve .jpg.
When i change order of try_files arguments from:
/imgs/$1$webp_suffix /imgs/$1.jpg
/imgs/$1.jpg /imgs/$1$webp_suffix
than it works too and serve .jpg

I have added "add_header" to check if map webp detection works and it
results in:
X-webp .webp
header when webp support enabled in client, so map is working as expected.

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 ?

Marcin Wanat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list