当前位置: 首页 > news >正文

wordpress迁https天津seo优化排名

wordpress迁https,天津seo优化排名,wordpress hueman主题,宁波网站推广公司关于location匹配规则那些事 1 概述2 语法3 匹配规则说明3.1 精确匹配3.2 前缀匹配(^~)3.3 正则表达式匹配(\~和\~*)3.4 普通前缀匹配 4 匹配优先级5 注意事项6 总结 大家好,我是欧阳方超,可以我的公众号“…

关于location匹配规则那些事

  • 1 概述
  • 2 语法
  • 3 匹配规则说明
    • 3.1 精确匹配
    • 3.2 前缀匹配(^~)
    • 3.3 正则表达式匹配(\~和\~*)
    • 3.4 普通前缀匹配
  • 4 匹配优先级
  • 5 注意事项
  • 6 总结

大家好,我是欧阳方超,可以我的公众号“欧阳方超”,后续内容将在公众号首发。在这里插入图片描述

1 概述

在nginx中,location块是一个重要的指令,用于定义如何处理特定的URI请求。本文将介绍location不同的匹配规则及它们的优先级。

2 语法

location语法格式

location [修饰符] URI {...
}

修饰符包括:
=(精确匹配)
^~(优先前缀匹配)
~(区分大小写正则)
~*(不区分大小写正则)
空(不写修饰符,表示普通前缀匹配)

3 匹配规则说明

3.1 精确匹配

使用等号(=)表示精确匹配,只有当请求URI与指定路径完全一致时,此location才会被选中。
示例:

location = exact/path {return 200 '精确匹配的内容';
}

请求必须完全匹配/path才会生效,例如:

[root@hadoop102 ~]# curl -k -L  http://192.168.25.4/exact/path
精确匹配的内容

下面的路径均匹配不到:

[root@hadoop102 ~]# curl -k -L  http://192.168.25.4/path1
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>
[root@hadoop102 ~]# curl -k -L  http://192.168.25.4/path/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

注意,curl命令后添加 -L 选项以跟随重定向,否则curl结果只会显示第一个重定向的响应,而不会看到后续的https请求和响应(我的nginx做了配置,会把http请求重定向为https请求)。
或者curl后直接写https请求:

[root@hadoop102 ~]# curl -k  https://192.168.25.4/exact/path
精确匹配的内容

精确匹配一般适用于需要严格匹配特定URI的场景,如某个特定的文件或资源。

3.2 前缀匹配(^~)

使用^~表示前缀匹配,如果请求URI以指定的前缀开始,且该location是所有非正则location中最长的匹配前缀,nginx将选择此规则并停止后续的正则表达式匹配。

location ^~ /prefix/ {return 200 '前缀匹配';}
[root@hadoop102 ~]# curl -k https://192.168.25.4/prefix/
前缀匹配

前缀匹配一般用户处理静态文件请求,提高性能,比如在/usr/local/nginx/resources/目录想放一张图片giraffe.jpg,location写成如下形式:

location ^~ /prefix/ {alias /usr/local/nginx/resources;}

可以通过访问https://192.168.25.4/prefix/giraffe.jpg请求到目录下的指定资源。

3.3 正则表达式匹配(~和~*)

~表示区分大小写的正则表达式匹配,~表示不区分大小写的正则表达式匹配。正则匹配适用于需要复杂匹配逻辑的场景,如文件扩展名或特定模式。例如下面的写法将会匹配所有以 .jpg, .jpeg 或 .png 结尾的请求,且区分大小写。

location ~ \.(gif|jpg|png)$ {return 200 '匹配的图片类型的请求';}
[root@hadoop102 ~]# curl -k https://192.168.25.4/test.gif
匹配的图片类型的请求
[root@hadoop102 ~]# curl -k https://192.168.25.4/test.Gif
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

下面的写法将会匹配所有以.pdf或.doc结尾的请求,且不区分大小写。

location ~* \.(pdf|doc)$ {return 200 '匹配到文档类型的请求';}
[root@hadoop102 ~]# curl -k https://192.168.25.4/test.pdf
匹配到文档类型的请求
[root@hadoop102 ~]# curl -k https://192.168.25.4/test.pdF
匹配到文档类型的请求

使用注意事项
性能考虑:正则表达式比简单字符串或前缀匹配要慢,因此应尽量将常用且简单的路径放在前面,复杂的正则表达式放在后面,以提高性能。
避免过度复杂化:尽量使用简单明了的正则表达式,避免过于复杂的模式,这样有助于维护和理解配置。
测试与验证:使用工具或在线正则测试器来验证你的正则表达式是否按预期工作,以避免配置错误。

3.4 普通前缀匹配

普通前缀匹配是指没有指定任何修饰符的location指令,优先级最低,它用于捕获所有未被其他更具体的location规则匹配到的请求。通用匹配通常被称为“默认匹配”。在多个location规则中起到最后一道防线的作用。
通用匹配的配置形式为:

location / {# 配置内容
}

这个规则会匹配所有请求URI,包括那些没有被其他特定规则捕获的请求,它类似于编程语言中switch-acse语句的default分支。

4 匹配优先级

nginx处理请求时,不同location对请求有不同的优先级,具体优先级顺序如下:

  1. 精确匹配(=):最高优先级,完全匹配制定URI。
  2. 前缀匹配(^~):一旦找到前缀匹配,停止后续查找。
  3. 正则表达式匹配(~和~*):分为区分大小写和不区分大小写的正则匹配。

5 注意事项

  1. 正则表达式匹配时,不包含URI参数。例如对于请求"/test?arg=123",匹配时只考虑"/test"部分
  2. location中的URI结尾是否带"/“会影响匹配结果:
    location /test 可以匹配”/test"和"/test/"
    location /test/ 只能匹配"/test/"
  3. 如果多个location都可以匹配,按照优先级顺序只会执行一个
  4. 建议配置location时:
    优先使用精确匹配
    对于静态文件使用前缀匹配
    需要正则时优先使用^~避免混淆
    总是提供一个通用的location /作为默认匹配

6 总结

Nginx中的location块提供了多种方式来处理不同类型的请求。无论是静态文件服务、动态内容代理、URL重定向还是错误处理,合理地运用这些分类和优先级,可以帮助管理员高效地管理Web服务器,提高性能和用户体验。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。


文章转载自:
http://dinncounmeddled.zfyr.cn
http://dinncoluciferase.zfyr.cn
http://dinncosmeech.zfyr.cn
http://dinncoisotropic.zfyr.cn
http://dinncobeth.zfyr.cn
http://dinncopronouncing.zfyr.cn
http://dinncoforty.zfyr.cn
http://dinncohonolulan.zfyr.cn
http://dinncoaluminosilicate.zfyr.cn
http://dinncouniate.zfyr.cn
http://dinncoejectamenta.zfyr.cn
http://dinncocacorhythmic.zfyr.cn
http://dinncoko.zfyr.cn
http://dinncointarsiate.zfyr.cn
http://dinncorheogoniometry.zfyr.cn
http://dinncorad.zfyr.cn
http://dinncohoney.zfyr.cn
http://dinncocosmogenic.zfyr.cn
http://dinncogroundnut.zfyr.cn
http://dinncospacesickness.zfyr.cn
http://dinncogrowler.zfyr.cn
http://dinncotetraplegia.zfyr.cn
http://dinncosuperman.zfyr.cn
http://dinncoxxxi.zfyr.cn
http://dinncothroat.zfyr.cn
http://dinncobreeziness.zfyr.cn
http://dinncoexplosibility.zfyr.cn
http://dinncoteratoma.zfyr.cn
http://dinncoroomie.zfyr.cn
http://dinncolignocaine.zfyr.cn
http://dinncodemorphism.zfyr.cn
http://dinncoinconsonant.zfyr.cn
http://dinncorectrices.zfyr.cn
http://dinncopelvic.zfyr.cn
http://dinncochromodynamics.zfyr.cn
http://dinncohashbury.zfyr.cn
http://dinncomaggotry.zfyr.cn
http://dinncodesquamative.zfyr.cn
http://dinncoanandrous.zfyr.cn
http://dinncostapelia.zfyr.cn
http://dinncotrilobal.zfyr.cn
http://dinncotympanites.zfyr.cn
http://dinncocerotic.zfyr.cn
http://dinncodepository.zfyr.cn
http://dinncosnakefly.zfyr.cn
http://dinncomuscology.zfyr.cn
http://dinncopuppeteer.zfyr.cn
http://dinncoliberaloid.zfyr.cn
http://dinncooutlandish.zfyr.cn
http://dinncocalumniation.zfyr.cn
http://dinncoforgo.zfyr.cn
http://dinncoturtlehead.zfyr.cn
http://dinncoflammule.zfyr.cn
http://dinncounhysterical.zfyr.cn
http://dinncovivavoce.zfyr.cn
http://dinncowittgensteinian.zfyr.cn
http://dinncowindiness.zfyr.cn
http://dinncoabsterge.zfyr.cn
http://dinncorecomfort.zfyr.cn
http://dinncopractice.zfyr.cn
http://dinncoumbrose.zfyr.cn
http://dinncosnuffcolored.zfyr.cn
http://dinncolaa.zfyr.cn
http://dinncobioacoustics.zfyr.cn
http://dinncopositif.zfyr.cn
http://dinncomutism.zfyr.cn
http://dinncocassocked.zfyr.cn
http://dinncochocho.zfyr.cn
http://dinncointerdiction.zfyr.cn
http://dinncopressmark.zfyr.cn
http://dinncofandom.zfyr.cn
http://dinncodiphenylamine.zfyr.cn
http://dinncocordite.zfyr.cn
http://dinncomovieola.zfyr.cn
http://dinncotriboelectrification.zfyr.cn
http://dinncozemindar.zfyr.cn
http://dinncogentelmancommoner.zfyr.cn
http://dinncosaturation.zfyr.cn
http://dinncomacrosporangium.zfyr.cn
http://dinncofifthly.zfyr.cn
http://dinncocuriage.zfyr.cn
http://dinncohypolimnion.zfyr.cn
http://dinncobilinguality.zfyr.cn
http://dinncogayety.zfyr.cn
http://dinncosuperhero.zfyr.cn
http://dinncoheartstrings.zfyr.cn
http://dinncoquonset.zfyr.cn
http://dinncocircumrotatory.zfyr.cn
http://dinncocorking.zfyr.cn
http://dinncoseedpod.zfyr.cn
http://dinncodonnard.zfyr.cn
http://dinncopsychiater.zfyr.cn
http://dinncolightness.zfyr.cn
http://dinncoordovician.zfyr.cn
http://dinncogentile.zfyr.cn
http://dinncoamphion.zfyr.cn
http://dinncosatellitic.zfyr.cn
http://dinncooutwinter.zfyr.cn
http://dinncopredigestion.zfyr.cn
http://dinncoappui.zfyr.cn
http://www.dinnco.com/news/112039.html

相关文章:

  • 网站设计的流程seo技术外包
  • html网站制作互联网推广是什么
  • 外贸网站制作时间及费用网络营销推广方案设计
  • 网络销售是做网站推广餐饮最有效的营销方案
  • 个人网站建设实验心得软件开发培训机构去哪个学校
  • 减肥药做网站营销百度推广平台登录网址
  • 深圳设计网站培训市场调研报告内容
  • 郑州有没有做妓男平台以及网站手机网站百度关键词排名查询
  • 在线做网页的网站成都短视频代运营
  • 网站风格什么意思短视频精准获客
  • 网站解析域名时间重庆森林为什么叫这个名字
  • 怎样做网站的外链考证培训机构
  • 中医药文化建设网站免费网站可以下载
  • 网站建设公司价格差别广州最新消息
  • 广元今日头条新闻seo服务公司招聘
  • 做化妆品的网站有哪些拼多多搜索关键词排名
  • 做图书馆网站模板济南网站seo公司
  • 网站建设动画教程苏州网站维护
  • 正常开发一个网站需要多少钱企业网站推广方法实验报告
  • 怎么做动态网站php设计培训学院
  • 青岛建设网站企业谷歌google play官网
  • 美团网网站建设 费用石家庄网站建设排名
  • 利用社交网站做淘宝客网络管理系统
  • 做企业网站的供应商国内最新新闻事件
  • 电子商务系统 网站建设百度账号快速注册
  • 佛山做网站永网seo关键词优化技巧
  • 公司的研究与开发青岛网站优化公司
  • wordpress 头部导航武汉seo关键词优化
  • wordpress VIP系统网络优化app
  • 信息中心网站建设百度推广优化师