2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

nginx try_filesディレクティブ

Posted at

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

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?