NGINX: map e try_files non funzionano

Oct 08 2020

sto usando nginx per rilevare il supporto webp nel browser:

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

questo codice funziona bene e assegna .webp a $ webp_suffix quando il client supporta image / webp

poi poche righe dopo sto usando questa variabile nella seguente configurazione:

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

e SEMPRE risulta in e404 anche se esistono entrambi i file. L'intestazione X-webp è impostata su ".webp" quindi la mappa funziona correttamente.

Cosa c'è di più strano - se cambio l'ordine degli argomenti di try_files da:

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

per

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

quindi funziona e ha inviato il file / imgs / $ 1 $ 3 al browser. Come è possibile ? Mi manca qualcosa in questa logica?

EDIT1: Completa nginx.conf per riprodurre:

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: Cosa c'è di ancora più strano - se cambio:

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

per:

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

quindi try_files funziona come previsto (ma ora non stiamo controllando il supporto webp nel client, semplicemente assumendo che lo supporti). Ricorda che:

add_header X-webp $webp_suffix;

nella stessa posizione si ottiene un'intestazione aggiunta: X-webp .webp

quindi perché / imgs / $ 1$webp_suffix does not work and /imgs/$1.webp funziona?

Cosa ho testato:

  1. $ webp_suffix = ".webp" (testato e funziona)
  2. try_files / imgs / $ 1$webp_suffix /imgs/$1 $ 3 = 404 - NON funziona e restituisce 404
  3. try_files /imgs/$1.webp / imgs /$1$3 = 404 - funziona e restituisce il file webp
  4. try_files / imgs /$1$3 / imgs /$1$webp_suffix = 404 - funziona e restituisce il file jpg

il che non ha alcun senso

EDIT3:

Per il test sto aprendo: http: // test: 8888 / imgs / 10 / qualunque.jpg

I file /srv/imgs/10.jpg e /srv/imgs/10.webp esistono.

Risposte

7 RichardSmith Oct 08 2020 at 00:50

L' try_filesistruzione utilizza acquisizioni numeriche dall'espressione regolare locationnell'istruzione. Il problema con le acquisizioni numeriche è che vengono sovrascritte ogni volta che viene valutata un'espressione regolare. Il mapcontiene un'espressione regolare che viene valutata tra il locatione le try_filesdichiarazioni.

La soluzione è utilizzare acquisizioni denominate:

Per esempio:

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