雑多なブログ

音楽や語学、プログラム関連の話題について書いています

nginx: try_filesの使い方

初めてtry_filesの記述を見た際、ApacheのDirectoryIndexと同じかな?と思っていたのですが、若干勘違いしていました(DirectoryIndexに相当するのは、indexディレクティブ?)。

例)

try_files $uri $uri/ =404;

上記設定は、$uri、$uri/ の順に見ていって、指定したパスにファイルが存在しない場合は404ステータスを返す、という挙動になります。

公式のドキュメントで構文を確認してみますと・・・

Syntax:  try_files file ... uri;
try_files file ... =code;
Default:    —
Context:    server, location

url: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

If none of the files were found, an internal redirect to the uri specified in the last parameter is made.

最後のパラメーターにuriを指定した場合、uriへの内部リダイレクトが発生するとのことです。