網站首頁 編程語言 正文
這一篇將簡單說一下Nginx的location功能。
1.Nginx location
1.1.location作用
location指令的作用就是根據用戶請求的URI來執行不同的應用。
1.2.location語法
location [ = | ~ | ~* | ^~ ] uri {...}
將以上語法分為四部分進行說明:
location:指令
[ = | ~ | ~* | ^~ ]:匹配的標識
uri:匹配的網站地址
{...}:匹配uri后要執行的配置段
注意:
與~*的區別是:~區分大小寫,~*不區分大小寫
^~:進行常規字符串匹配后,不做正則表達式的檢查
1.3.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
1.4.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.com
402
[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.html
401
[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/documents/documents.html
403
[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/images/1.gif
405
[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/documents/1.jpg
406
[root@nginx-01 ~]# curl -s -o /dev/null -I -w "%{http_code}\n" bbs.yygg.com/yyang/
401
?
返回的狀態碼也是與配置中的規則相匹配的。
1.5.不用uri及特殊字符組合匹配的順序說明
location =/ {}:精確匹配
location ^~ /images/ {}:常規字符串匹配,不做正則匹配
location ~* \.(gif|jpg|jpeg)$ {}:正則匹配
location /documents/ {}:常規字符串匹配,如果有正則,優先匹配正則
location / {}:所有location都不匹配后默認匹配
原文鏈接:https://yyang.blog.csdn.net/article/details/113842882
相關推薦
- 2022-05-11 React中的Refs屬性你來了解嗎_React
- 2022-07-29 pytest解讀一次請求多個fixtures及多次請求_python
- 2022-07-27 C++中this指針理解及作用_C 語言
- 2023-03-26 TypeScript?高級數據類型實例詳解_其它
- 2022-02-19 DevTools 無法加載 SourceMap 錯誤:狀態代碼 404,net::ERR_HTTP_
- 2022-05-10 torch.cuda.is_available()返回false最終解決方案
- 2022-03-14 使用npm安裝淘寶鏡像(npm配置淘寶鏡像)
- 2022-08-30 Token的生成、Token的作用及Base編碼的注意事項
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支