niginx的location的location详细规则和优先级关系以及测试案例

1.介绍

在nginx的server配置中,可以一个server具备多个location,利用匹配规则,多角度,多需求的匹配多种请求路径uri.这时,需要了解location详细规则和优先级关系

2.规则

2.1匹配语法

精确匹配:=/image
location =/image { location 逻辑}
请求uri路径必须 等于 /image
有修饰的字符串前缀:^~/image
location ^~/image { location 逻辑}
请求uri路径是 以/image开始的.一旦匹配到,处于优先级最高状态,其他规则一概不再匹配(停止匹配)
无修饰的字符串前缀: /image
location /image { location 逻辑}
请求uri路径是 以/image开始的.
正则匹配:
区分大小写 ~.png$ ~.Png$ ~.pNg$
不区分大小写: ~.png ∣   ∗ . P n g |~*.Png  .Png
请求路径uri只要满足正则就能匹配到
location ~
.png$ { location 逻辑}
通配: /
匹配到所有当前server的请求

2.2优先级判断

上述各种匹配规则,使得如果server中location规则非常多,容易出现包含关系,必定要判断优先级
上述顺序从上到下优先级依次降低
精确匹配>有修饰>无修饰>正则>通配
同种规则有包含关系
字符串前缀匹配
原则:最大匹配长度,优先级最高(精度越高,优先级越高)
正则
原则:谁配置location在上,加载更靠前,优先级更高

2.3练习

server {
    
    
       listen 80;
       server_name www.easy.com;

location ^~/image {
    
    return 200}
location /image {
    
    return 201}
location ^~/image/easymall {
    
    return 202}
location =/image/ {
    
    return 203}
location ~*.png$ {
    
    return 204}
location ~.pNg$ {
    
    return 205}
location ~.(png|jpg|gif|jpeg)$ {
    
    return 206}
location / {
    
    return 207}
}

请求url uri 返回值
http://www.easy.com / 207
http://www.easy.com/image /image 203
http://www.easy.com/image/easy /image/easy 200
http://www.easy.com/image/easy.pNg /image/easy.pNg 200
http://www.easy.com/image/easymall.png /image/easymall.png 202
http://www.easy.com/haha.png /haha.png 204

和字符串匹配有关的 前缀,精确,这些都会把uri中的路径过滤掉,使用剩余的uri拼接proxy_pass,能够起到保护后端接口资源的功能
server {
listen 80;
server_name www.ou.com;
location /hello {
proxy_pass http://localhost:8090/;
}
}
浏览器请求发起:http://www.ou.com/hello
|进入nginx 匹配server
重要 | uri /hello url-patter /hello 匹配过滤了hello,uri剩下/
最终响应数据:http://localhost:8090/
这样的过滤规则,是为了保护后端的接口

需求说明
使用nginx将请求转发到后端服务器
在这里插入图片描述配置nginx.conf
在nginx的根目录的conf文件夹下,找到nginx.conf
http{ 当前nginx的所有http服务的功能
server{} 每一个server都代表一个虚拟服务器,每个server都能处理不同请求
server{}
server{}
server{}
….
}

3 编写一个server结构

#一个server表示一个虚拟服务器 一个server跟一个独立tomcat类似
server {
    
    
#当前虚拟服务器监听的端口 多个nginx server可以监听同一个端口
listen 80;
#虚拟服务器的域名 不可以多个server重复
server_name www.ou.com;
#处理请求的访问逻辑,请求应该怎么交给后端的tomcat处理在location决定,一个server下可以有若干个location location url-pattern www.ou.com/hello;www.ou.com/a/b/c/d
location / {
    
    
#/表示通配 当别的匹配url-pattern 的location全部不满足时,/必定满足
proxy_pass http://localhost:8090/;
}
}
 

nginx作为一个http服务器,会在启动时加载nginx.conf配置文件
加载到http下的多个server的内容,每一个server会被nginx启动为一个线程实现http服务器功能–接收,发送请求
访问nginx的请求非常多,多个server,某一次请求到底谁来处理.
server内容的配置决定的某次请求交给哪个虚拟服务器server
listen :当前server在nginx服务器中监听的端口号,只要有一个server配置了这个端口号的监听,nginx将这个端口占用使用.
在这里插入图片描述是否请求到达nginx之后,只要满足端口号的监听,就能确定当前请求是哪个server处理吗?
server_name 通过它来判断这次请求的域名携带的是谁
计算机生成了可选文字:www.test1.comwww.test2.comwww.test3.com0818080serverlisten80
servername域名1serverlisten80servername域名2

在这里插入图片描述server中的 listen和server_name来决定某一个请求到nginx的访问是否交给该虚拟服务器server处理,多个server不能有相同的端口同时还有相同的域名

3.1处理逻辑如何执行

location 用来比对当前这个请求uri地址,可以在同一个server中配置多个location比对uri的结构,满足结构的,优先级最高的location负责处理这次请求具体逻辑
uri地址: /a/b/c
url地址: http://www.ou.com/a/b/c
location / 表示只要uri地址字符串是以 “/” 开头的,就匹配上了这个location.看成是个通配,因为任何uri地址都会以"/"开始
proxy_pass 处理动态数据的转发 (静态数据转发 root)
测试案例
server{
listen 80;
server_name www.ou.com;
location / {
proxy_pass http://127.0.0.1:8090/;
}
}

nginx启动后会加载这个server,server来监听80端口,到达80端口请求判断域名是否是www.ou.com,满足则开始判断location ,转发到proxy_pass 8091工程

3.2功能测试

保证一个启动在8090的springboot运行
启动工程
保证www.ou.com能够访问到nginx 配置hosts文件
127.0.0.1 表示的是nginx的服务器ip
127.0.0.1 www.ou.com
nginx正常启动
在这里插入图片描述

3.3流程解析

客户端发起地址:http://www.ou.com/hello
|域名的ip地址映射 127.0.0.1:80 进入nginx服务器
进nginx**
|多个server 判断请求端口和请求域名 www.ou.com
|交给一个虚拟服务器server处理
|匹配location的url-pattern /hello 匹配 / 匹配成功
|进入location 内部 交给proxy_pass 定位真正的后端服务器
|/hello 拼接到 http://localhost:8090/hello
出nginx**
最终响应的接口:http://localhost:8090/hello

课堂案例:流程解析
客户端发起地址:http://www.ou.com/get/user
|进入nginx 域名 ip映射,ip地址nginx服务器ip
|server 匹配location /get/user 匹配 /
|proxy_pass 拼接请求 http://localhost:8090/get/user
最终响应的接口:http://localhost:8090/get/user

猜你喜欢

转载自blog.csdn.net/qq_41536934/article/details/112330330