
Ich habe Probleme, die Routing-Verarbeitung in Nginx zu verstehen.
server {
listen 80;
listen [::]:80;
server_name my_domain;
root /var/www/ep;
index index.html;
error_page 403 404 /my403.html;
location /images/ {
try_files $uri @not_found;
}
location @not_found {
root /var/www/ep/not_found;
try_files NON_EXISTENT_1 /not_found.jpg;
}
}
Für die obige Konfiguration, wenn ich besuchehttp://meine_domain/images/xyz.pngoderhttp://meine_domain/bilder/Ich erwarte, dass @not_found
die Standortanweisung ausgeführt wird und ein Bild zurückgibt /var/www/ep/not_found/not_found.jpg
(es existiert), aber irgendwie /var/www/ep/not_found.jpg
wird der Pfad ausprobiert (er existiert nicht). Das Ergebnis ist die Anzeige /my403.html
einer Datei.
Ich möchte den benannten Standortansatz ( @not_found
) verwenden.
Kann jemand erklären, was hier passiert?
Warum /var/www/ep/not_found/not_found.jpg
wird nicht bedient?
Antwort1
Sie platzieren es /not_found.jpg
am Ende der try_files
Anweisung, wodurch es der URI-Begriff wird, was bedeutet, nginx
dass nach einem neuen gesucht wird location
, um die Anfrage zu verarbeiten. Sie sollten es machennichtzuletzt, zum Beispiel:
try_files /not_found.jpg =404;
Sehendieses Dokumentfür mehr.
Als Alternative zu einem benannten location
können Sie Folgendes verwenden:
location /images/ {
try_files $uri /not_found.jpg;
}
location = /not_found.jpg {
root /var/www/ep/not_found;
}
Sehendieses Dokumentfür Details.