
Nginx でのルーティング処理を理解するのに問題があります。
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;
}
}
上記の設定では、訪問時にhttp://my_domain/images/xyz.pngまたはhttp://my_domain/images/@not_found
場所の指示が実行され、画像が返されることを期待していますが/var/www/ep/not_found/not_found.jpg
(存在します)、何らかの理由で/var/www/ep/not_found.jpg
パスが試行されています (存在しません)。結果としてファイルが表示されます/my403.html
。
名前付きロケーションアプローチ( )を使用したいと思います@not_found
。
ここで何が起こっているのか誰か説明してくれませんか?
なぜ/var/www/ep/not_found/not_found.jpg
提供されないのですか?
答え1
/not_found.jpg
ステートメントの最後に配置しますtry_files
。これはURI用語となり、リクエストを処理するためにnginx
新しいものを検索することを意味します。location
ない最後に、例えば:
try_files /not_found.jpg =404;
見るこのドキュメント多くのための。
名前付き の代わりにlocation
、以下を使用できます。
location /images/ {
try_files $uri /not_found.jpg;
}
location = /not_found.jpg {
root /var/www/ep/not_found;
}
見るこのドキュメント詳細については。