本篇内容介绍了“Nginx的location功能怎么配置”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Nginx location 1.1.
本篇内容介绍了“Nginx的location功能怎么配置”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Nginx location 1.1.location作用
location指令的作用就是根据用户请求的URI来执行不同的应用。
location语法
location [ = | ~ | ~* | ^~ ] uri {...}
将以上语法分为四部分进行说明:
location:指令[ = | ~ | ~* | ^~ ]:匹配的标识uri:匹配的网站地址{...}:匹配uri后要执行的配置段
注意:
~与~*的区别是:~区分大小写,~*不区分大小写^~:进行常规字符串匹配后,不做正则表达式的检查
location匹配示例
location = / {[ configuration A]}location / {[ configuration B]}location /documents {[ configuration C]}location ^~ /images/ {[ configuration D]}location ~* \.(gif|jpg|jpeg)$ {[ configuration E]}
上述配置,请求“/”时,匹配configuration A请求"/index.html"时,讲匹配configuration B请求“/documents/docunment.html”时,匹配configuration C请求“images/1.gif”时,匹配configuration D请求“/documents/1.jpg”时,匹配configuration E
location配置实例
server { listen 80; server_name bbs.yygg.com; root html/bbs; location / { return 401; } location =/ { return 402; } location /documents/ { return 403; } location ^~ /images/ { return 405; } location ~* \.(gif|jpg|jpeg)$ { return 406; }
测试结果
[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{Http_code}\n" bbs.yygg.com402[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/402[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/index.html401[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/documents/documents.html403[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/images/1.gif405[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/documents/1.jpg406[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/yyang/401
返回的状态码也是与配置中的规则相匹配的。
不用uri及特殊字符组合匹配的顺序说明
location =/ {}:精确匹配location ^~ /images/ {}:常规字符串匹配,不做正则匹配location ~* \.(gif|jpg|jpeg)$ {}:正则匹配location /documents/ {}:常规字符串匹配,如果有正则,优先匹配正则location / {}:所有location都不匹配后默认匹配
“Nginx的location功能怎么配置”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
--结束END--
本文标题: Nginx的location功能怎么配置
本文链接: https://www.lsjlt.com/news/325167.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0