nginx 平滑升级、location、访问控制

root 48938 1494 0 00:50 pts/0 00:00:00 grep --color=auto nginx

location配置


location区段,通过指定模式来与客户端请求的URI相匹配

//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能

//语法:location [ 修饰符 ] pattern {…}

常用修饰符说明:

| 修饰符 | 功能 |

| :-: | :-- |

| = | 精确匹配 |

| ~ | 正则表达式模式匹配,区分大小写 |

| ~* | 正则表达式模式匹配,不区分大小写 |

| ^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |

| @ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |

没有修饰符表示必须以指定模式开始,如:

[root@localhost local]# vim nginx/conf/nginx.conf

location / {

root html;

index index.html index.htm;

}

location /test {

echo “test”;

}

[root@localhost ~]# nginx -s reload

那么如下内容就可正确匹配:

[root@localhost ~]# curl http://192.168.129.33/test

test

[root@localhost ~]# curl http://192.168.129.33/test/

test

[root@localhost ~]# curl http://192.168.129.33/test?test

test

=:表示必须与指定的模式精确匹配,如:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location / {

root html;

index index.html index.htm;

}

location /test { #匹配/test下的所有

echo “test”;

}

location =/test {

echo “111”;

}

[root@localhost ~]# nginx -s reload

那么如下内容就可正确匹配:

[root@localhost ~]# curl http://192.168.129.33/test

111

如下内容则无法匹配:

[root@localhost ~]# curl http://192.168.129.33/test/

test

[root@localhost ~]# curl http://192.168.129.33/test/hh

test

[root@localhost ~]# curl http://192.168.129.33/testtest

test

~:表示指定的正则表达式要区分大小写,如:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location / {

root html;

index index.html index.htm;

}

location /test {

echo “test”;

}

location ~ ^/test$ {

echo “大小写”;

}

[root@localhost ~]# nginx -s reload

那么如下内容就可正确匹配:

[root@localhost ~]# curl http://192.168.129.33/test

大小写

如下内容则无法匹配:

[root@localhost ~]# curl http://192.168.129.33/test/

test

[root@localhost ~]# curl http://192.168.129.33/testkllk

test

~*:表示指定的正则表达式不区分大小写,如:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location ~ ^/test$ {

echo “大小写”;

}

[root@localhost ~]# nginx -s reload

那么如下内容就可正确匹配:

[root@localhost ~]# curl http://192.168.129.33/test

不分大小写

[root@localhost ~]# curl http://192.168.129.33/TEST

不分大小写

[root@localhost ~]# curl http://192.168.129.33/TEst

不分大小写

如下内容则无法匹配:

[root@localhost ~]# curl http://192.168.129.33/TEst/

404 Not Found

404 Not Found


nginx/1.21.3

[root@localhost ~]# curl http://192.168.129.33/testas

404 Not Found

404 Not Found


nginx/1.21.3

~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location / {

root html;

index index.html index.htm;

}

location /test {

echo “无”;

}

location ~ ^/test$ {

echo “分大小写”;

}

location ~* ^/test$ {

echo “不分大小写”;

}

[root@localhost ~]# nginx -s reload

那么如下内容就可正确匹配:

[root@localhost ~]# curl http://192.168.129.33/test

分大小写

[root@localhost ~]# curl http://192.168.129.33/tesT

不分大小写

[root@localhost ~]# curl http://192.168.129.33/test/

[root@localhost ~]# curl http://192.168.129.33/test/asda

[root@localhost ~]# curl http://192.168.129.33/testasda

查找顺序和优先级:由高到底依次为

  1. 带有=的精确匹配优先

  2. 正则表达式按照他们在配置文件中定义的顺序

  3. 带有^~修饰符的,开头匹配

  4. 带有~~*修饰符的,如果正则表达式与URI匹配

  5. 没有修饰符的精确匹配

优先级次序如下:

( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( locatio

访问控制


用于location段

allow:设定允许哪台或哪些主机访问,多个参数间则换行

deny:设定禁止哪台或哪些主机访问,多个参数间则换行

示例:

allow 192.168.1.1/32 ;

allow 192.168.2.1/32 ;

deny all;

示例:

[root@localhost ~]# mkdir /usr/local/nginx/html/test -p

[root@localhost ~]# cat > /usr/local/nginx/html/test/index.html >>EOF

test page

baidu

EOF

[root@localhost ~]# nginx -s reload

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location / {

root html;

index index.html index.htm;

}

location /test {

deny 192.168.129.1; ## 黑名单(除了自己谁都能访问)

root html;

index index.html;

}

[root@localhost ~]# curl http://192.168.129.33/test/index.html

test page

baidu

在这里插入图片描述

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location / {

root html;

index index.html index.htm;

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
。**

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-n8ppqEQg-1715855477817)]

[外链图片转存中…(img-rPLoRoKV-1715855477817)]

[外链图片转存中…(img-kQDu9036-1715855477818)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值