Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Location in Nginx

Shulou Source: shulou.com Published: 2022-06-02 10:40:37 10月04日 Update

This article introduces how to use Location in Nginx, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Location expression type

~ means to perform a regular match that is case sensitive

~ * means to perform a regular match, which is case-insensitive

^ ~ indicates a normal character match. Use prefix matching. If the match is successful, no other location will be matched

= exact matching of ordinary characters. That is, it is a perfect match.

Location priority

The order of location in the location and configuration of nginx does not have much to do with the type of location expression. For expressions of the same type, long strings will match first.

Here are the instructions in order of priority:

The equal sign type (=) has the highest priority. Once the match is successful, no other matches are found.

^ ~ type expression. Once the match is successful, no other matches are found.

The regular expression type (~ *) takes second priority. If the regularities of multiple location can match, the one with the longest regular expression is used.

Regular string matching type. Match by prefix.

Example

The configuration items are as follows:

Location = / {# only match requests / [configuration A]} location / {# match all requests that start with /. # but if there is a longer expression of the same type, choose a longer expression. # if there is a regular expression to match, then match the regular expression first. [configuration B]} location / documents/ {# matches all requests that start with / documents/. # but if there is a longer expression of the same type, choose a longer expression. # if there is a regular expression to match, then match the regular expression first. [configuration C]} location ^ ~ / images/ {# matches all expressions that begin with / images/, and if the match succeeds, stops the match search. # so, even if there is a matching regular expression location, it will not be used [configuration D]} location ~ *\. (gif | jpg | jpeg) ${# matches all requests ending in gif jpg jpeg. # but requests that start with / images/ will use Configuration D [configuration E]}

Example of request matching:

/-> configuration A/index.html-> configuration B/documents/document.html-> configuration C/images/1.gif-> configuration D/documents/1.jpg-> configuration E is here about how to use Location in Nginx. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Tags: Expression regular type success priority character beginning normal content prefix size string more examples help selection configuration good longest highest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Docker Shulou Technology Apple Microsoft