nginx try_filesディレクティブ調べる。
try_filesディレクティブにはファイルを指定する。複数ファイルを指定することも可能。
左から指定されたファイルを探し、ファイルが存在した場合はそのファイルをレスポンスとして返す。
最後のパラメータはファイルではなくuriとして扱い、全てのファイルが存在しなかった場合はuriを使って内部リダイレクトを行う。
シンタックス
try_files file ... uri;
try_files file ... =code;
↓こうするとどのpathがきても/hoge/hoge.txtが返される。
index /hoge/hoge.txtのおかげ?でpathが/で終わる場合に/hoge/hoge.txtで内部リダイレクトする。
locationは/しか定義していないので再度/のlocationの処理が行われる。
try_filesでは$uriが指定されているので/hoge/hoge.txtのファイルを探しレスポンスとして返す。
location / {
add_header uri $uri;
index /hoge/hoge.txt;
try_files $uri /hoge/hoge.txt;
}
/var/log/nginx/error.log
2020/12/12 13:42:30
[error] 1010#0: *164 rewrite or internal redirection cycle while internally redirecting to "////////////",
client: 127.0.0.1, server: _, request: "HEAD / HTTP/1.1", host: "localhost"
公式
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files