Nginxのlocationディレクティブの例

NGINXのサーバーブロック内のlocationディレクティブは、リクエストを正しい場所にルーティングすることができます。このディレクティブは、ファイルやフォルダを含むリソースの場所をNGINXに伝えるために使用され、URLに対してlocationブロックをマッチングさせながら、どこを探すかを指定します。このチュートリアルでは、NGINXのlocationディレクティブについて詳しく見ていきます。

必要条件 (hitsuyōjōken)

  • You have already installed NGINX by following our tutorial from here.

NGINXのロケーションディレクティブの構文は次の通りです。

NGINXのlocationブロックは、いくつかの制限付きで、サーバーブロックの内部または別のlocationブロックの内部に配置することができます。locationブロックの構築のための構文は次の通りです。

location [modifier] [URI] {
  ...
  ...
}

場所ブロック内の修飾子はオプションです。場所ブロックに修飾子を含めると、NGINXはURLを異なる方法で処理できます。最も一般的な修飾子はいくつかありますが、

  • none: If no modifiers are present in a location block then the requested URI will be matched against the beginning of the requested URI.
  • =: The equal sign is used to match a location block exactly against a requested URI.
  • ~: The tilde sign is used for case-sensitive regular expression match against a requested URI.
  • ~*: The tilde followed by asterisk sign is used for case insensitive regular expression match against a requested URI.
  • ^~: The carat followed by tilde sign is used to perform longest nonregular expression match against the requested URI. If the requested URI hits such a location block, no further matching will takes place.

NGINXは、どのようにlocationブロックを選択しますか?

URIに対して場所の一致を見つけるために、NGINXは次の手順で場所ブロックを選択します。まず、正規表現を使用せずに接頭辞の文字列を使用して定義された場所をスキャンします。その後、正規表現を使用して定義された場所が設定ファイル内の宣言の順番でチェックされます。場所を定義するためには、接頭辞の文字列や正規表現を使用することができます。大文字小文字を区別しない正規表現は、「~*」修飾子を前に指定し、大文字小文字を区別する正規表現には「~」修飾子を使用します。

  • NGINX starts with looking for an exact match specified with location = /some/path/ and if a match is found then this block is served right away.
  • If there are no such exact location blocks then NGINX proceed with matching longest non-exact prefixes and if a match is found where ^~ modifier have been used then NGINX will stop searching further and this location block is selected to serve the request.
  • If the matched longest prefix location does not contain ^~ modifier then the match is stored temporarily and proceed with following steps.NGINX now shifts the search to the location block containing ~ and ~* modifier and selects the first location block that matches the request URI and is immediately selected to serve the request.
    If no locations are found in the above step that can be matched against the requested URI then the previously stored prefix location is used to serve the request.

以下は、NGINXのlocationブロックの例です。

NGINXのmodifierとURIを使用したlocationブロックの数例を挙げましょう。

全リクエストに一致するNGINXのロケーションマッチ

以下の例では、接頭辞の場所/はすべてのリクエストにマッチしますが、一致が見つからない場合にのみ最終手段として使用されます。

location / {
    ...
}

2. NGINXの場所を正確なURLに合わせる。

NGINXは常に最も具体的なプレフィックスの場所に一致しようとします。したがって、次のロケーションブロックのイコール記号は、要求されたパスと完全に一致し、それ以上の一致を探すのをやめさせます。

location = /images { 
    ...
}

上記の場所のブロックは、URL https://domain.com/images と一致しますが、URL https://domain.com/images/index.html や https://domain.com/images/ は一致しません。

3. ディレクトリのためのNGINXのlocationブロック

次の場所ブロックは、/images/で始まるすべてのリクエストに一致し、さらに具体的なブロックを検索して要求されたURIに適合します。そのため、NGINXがより具体的な一致を見つけることができない場合に、この場所ブロックが選択されます。

location /images/ {
     ...
     ...
}

4. NGINXの場所の正規表現の例

下記の場所ブロックの修飾子「^~」は、大文字と小文字の区別がある正規表現の一致を生じます。そのため、URI「/images」や「/images/logo.png」は一致しますが、一致が見つかるとすぐに検索が停止します。

location ^~ /images {
   ...
   ...
}

画像、CSS、JSファイルタイプのためのNGINXの場所ブロック

次のlocationブロック内の修飾子~*は、png、ico、gif、jpg、jpeg、css、またはjsで終わるすべてのリクエストに一致しますが、/images/フォルダへのリクエストは前のlocationブロックによって処理されます。

location ~* .(png|ico|gif|jpg|jpeg|css|js)$ {
    ...
    ...
}

6. NGINXの場所の正規表現は大文字と小文字を区別するマッチさせます。

以下の場所ブロック内の修飾子~は、大文字と小文字を区別する正規表現のマッチングを引き起こしますが、より適したマッチングが見つかるまで検索を停止しません。

location ~ /images {
    ...
    ...
}

7. NGINXの場所正規表現での大文字小文字を無視したマッチの例

以下の場所ブロック内の修飾子〜* によって、大文字小文字を区別しない正規表現の一致が生じますが、より良い一致を求めて検索はここで停止しません。

location ~* /images {
     ...
     ...
}

要約

URIのリクエストのエンドポイントをファイルシステムで追跡するために、NGINXのlocationディレクティブを理解することは重要です。この記事で説明されている修飾子、locationブロックの選択手順、およびいくつかの例は、NGINXのlocationブロックの始め方を容易にするのに役立ちます。

コメントを残す 0

Your email address will not be published. Required fields are marked *