`root`ディレクティブが機能するために`try_files`が必要なのはなぜですか?

`root`ディレクティブが機能するために`try_files`が必要なのはなぜですか?

私はnginxがフロントエンドで静的ファイルを見つけるのに苦労していました。try_filesすべてがうまくいくように、見かけにサーモンの繰り返しディレクティブを追加しました。

  location /frontend {
      try_files $uri /;
  }

  root {{ static_root }};
  index /frontend/index.html;

  location / {
      rewrite ^.*$ /frontend/index.html break;
  }

私の質問は:最初のディレクティブがなければ、なぜこれが機能しないのですかlocation /frontend

ベストアンサー1

これはrewrite無差別であり、すべての要求(.cssファイルを含む.js)に適用されます。別のlocationURIを追加すると、/frontend始まるURIを防ぐことができますrewrite

以下を使用して同様の結果を得ることができます。

root /path/to/root;

location / {
    try_files $uri $uri/ /frontend/index.html;
}

バラよりこのファイルもっと学ぶ。

おすすめ記事