大网数据广告活动
服务器运维问答

NGINX: map and try_files not working

· 52CE 我爱测

i am using nginx to detect webp support in browser: # Check if client is capable of handling webp map $http_accept $webp_suffix { default ""; "~*webp" ".webp"; } this code works ok and assign .webp to $webp_suffix when client sup…

问题原文

原帖链接 · forke · CC BY-SA 4.0

i am using nginx to detect webp support in browser:

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

this code works ok and assign .webp to $webp_suffix when client support image/webp

then few lines later i am using this variable in following config:

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

and it ALWAYS results in e404 even if both files exist. Header X-webp is set to ".webp" so map is working fine.

What is more weird - if i swap order of try_files arguments from:

try_files /imgs/$1$webp_suffix /imgs/$1$3 =404;

to

try_files /imgs/$1$3 /imgs/$1$webp_suffix =404;

then it do work and sent file /imgs/$1$3 to the browser. How is it possible ? Am i missing something in this logic ?

EDIT1: Complete nginx.conf to reproduce:

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$3 =404;
            }

    }
}

EDIT2: What is even more weird - if i change:

try_files /imgs/$1$webp_suffix /imgs/$1$3 =404;

to:

try_files /imgs/$1.webp /imgs/$1$3 =404;

then try_files works as expected (but we are not checking webp support in client now, simply assuming that it do support it). Please remember that:

add_header X-webp $webp_suffix;

in the same location results in added header: X-webp .webp

so why /imgs/$1$webp_suffix does not work and /imgs/$1.webp do work ?

What i have tested:

  1. $webp_suffix = ".webp" (tested and it works)
  2. try_files /imgs/$1$webp_suffix /imgs/$1$3 =404 - does NOT work and return 404
  3. try_files /imgs/$1.webp /imgs/$1$3 =404 - does work and return webp file
  4. try_files /imgs/$1$3 /imgs/$1$webp_suffix =404 - does work and return jpg file

which makes no sense at all

EDIT3:

For test i am opening: http://test:8888/imgs/10/whatever.jpg

Files /srv/imgs/10.jpg and /srv/imgs/10.webp do exist.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

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;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-10-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。