Get the App
SLTechnology News&Howtos  ›  Servers  › 

Zero-based configuration tutorial for Location in Nginx

Shulou Source: shulou.com Published: 2022-06-02 03:43:51 09月27日 Update

basic knowledge

The matching order of location is "match regular first, then match ordinary".

The matching order of location is actually "match ordinary first, match regular". The reason for this misunderstanding is that regular matches override ordinary matches.

Nginx location configuration syntax

1. location [ = | ~ | ~* | ^~ ] uri { ... }

2. location @name { ... }

Location configuration can be configured in two ways

1. prefix + uri (string/regular expression)

2.@ + name

Prefix Meaning

=: Exact match (all must be equal)

~: Case sensitive

~*: ignore case

^~: Only match uri part

@: Internal Service Jump

Location Basics

1. Location is configured in the server block.

2. You can use different configurations (in location) to process different requests based on different URIs.

3. Location is sequential and will be processed by the first matching location.

Location Configuration demo

1.=,exact match

location = / { #rule} #matches to `http://www.example.com/` such a request.

2.~,case sensitive

location ~ /Example/ { #rule} #request example #http://www.example.com/Example/ [success] #http://www.example.com/example/ [failure]

3.~*,Case ignored

location ~* /Example/ { #rule} #the uri case is ignored #http://www.example.com/Example/ [success] #http://www.example.com/example/ [success]

4.^~,Matches only if they start with uri

location ^~ /img/ { #Rule} #Requests that start with/img/will match #http://www.example.com/img/a.jpg [success] #http://www.example.com/img/b.mp4 [success]

5.@,nginx internal jump

location /img/ { error_page 404@img_err; } location @img_err { #rule} #Requests that begin with/img/if the link's status is 404. It matches the @img_err rule.

summary

Location in Nginx is not as difficult to understand as you think, so don't be afraid. Find more information and try more. You'll get something.

reference

http://nginx.org/en/docs/http/ngx_http_core_module.html#locationUniform Resource Identifier

Well, the above is the whole content of this article, I hope the content of this article has a certain reference value for everyone's study or work, if you have questions, you can leave a message to exchange, thank you for your support.

Tags: Configuration rules success size regular different ordinary beginning order accuracy content prefixes Basics Basics knowledge parts reference processing Learning value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MySQL Microsoft Shulou Technology Xiaomi