How to configure Location from scratch in Nginx

王林
Release: 2023-05-21 16:22:06
forward
751 people have browsed it

Basic knowledge

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

The matching order of location is actually "match ordinary first, then match regular". The reason for the misunderstanding is: regular matching will overwrite ordinary matching

nginx location configuration syntax

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

## 2.

location @name { ... }

location can be configured There are two configuration methods

1. Prefix uri (string/regular expression)


2.@ name


Prefix meaning

=: Exact match (all must be equal)


~: Case sensitive


~*: Ignore case


^~: Just match the uri part


@: Internal service jump


location Basic knowledge

1.location is configured in the server block.


2. You can use different configurations (configured in location) according to different uris to handle different requests.


3.location is in order and will be processed by the first matching location.

location configuration demo
##1.=, exact match

  location = / {
   #规则
  }
  # 则匹配到 `http://www.example.com/` 这种请求。
Copy after login

2.~, case sensitive

  location ~ /example/ {
    #规则
  }
  #请求示例
  #http://www.example.com/example/ [成功]
  #http://www.example.com/example/ [失败]
Copy after login

3.~*, case is ignored

 location ~* /example/ {
    #规则
 }
 # 则会忽略 uri 部分的大小写
 #http://www.example.com/example/ [成功]
 #http://www.example.com/example/ [成功]
Copy after login

4.^~, only matches starting with uri

 location ^~ /img/ {
   #规则
 }
 #以 /img/ 开头的请求,都会匹配上
 #http://www.example.com/img/a.jpg [成功]
 #http://www.example.com/img/b.mp4 [成功]
Copy after login

5.@, nginx internal jump

 location /img/ {
  error_page 404 @img_err;
 }
 
 location @img_err {
  # 规则
 }
 #以 /img/ 开头的请求,如果链接的状态为 404。则会匹配到 @img_err 这条规则上。
Copy after login

The above is the detailed content of How to configure Location from scratch in Nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!