網站首頁 編程語言 正文
location 介紹 location是Nginx中的塊級指令(block directive),,location指令的功能是用來匹配不同的url請求,進而對請求做不同的處理和響應,這其中較難理解的是多個location的匹配順序,本文會作為重點來解釋和說明。 開始之前先明確一些約定,我們輸入的網址叫做請求URI,nginx用請求URI與location中配置的URI做匹配。
Nginx的HTTP配置主要包括三個區塊,結構如下: http { //這個是協議級別 include mime.types; default_type application/octet-stream; keepalive_timeout 65; gzip on; server { //這個是服務器級別 listen 80; server_name localhost; location / { //這個是請求級別 root html; index index.html index.htm; } } }
location區段
通過指定模式來與客戶端請求的URI相匹配,基本語法如下:location [=|~|~*|^~|@] pattern{……}
1、沒有修飾符 表示:必須以指定模式開始,如:
server { server_name baidu.com; location /abc { …… } } 那么,如下是對的:http://baidu.com/abchttp://baidu.com/abc?p1http://baidu.com/abc/http://baidu.com/abcde
那么,如下是對的:
http://baidu.com/abc
http://baidu.com/abc?p1
http://baidu.com/abc/
http://baidu.com/abcde
2、=表示:必須與指定的模式精確匹配
server { server_name sish location = /abc { …… } } 那么,如下是對的: http://baidu.com/abc http://baidu.com/abc?p1 如下是錯的: http://baidu.com/abc/ http://baidu.com/abcde
3、~ 表示:指定的正則表達式要區分大小寫
server { server_name baidu.com; location ~ ^/abc$ { …… } } 那么,如下是對的: http://baidu.com/abc http://baidu.com/abc?p1=11&p2=22 如下是錯的: http://baidu.com/ABC http://baidu.com/abc/ http://baidu.com/abcde
4、~* 表示:指定的正則表達式不區分大小寫
server { server_name baidu.com; location ~* ^/abc$ { …… } } 那么,如下是對的: http://baidu.com/abc http://baidu..com/ABC http://baidu..com/abc?p1=11&p2=22 如下是錯的: http://baidu..com/abc/ http://baidu..com/abcde
5、^~ 類似于無修飾符的行為,也是以指定模式開始,不同的是,如果模式匹配,
那么就停止搜索其他模式了。
6、@ :定義命名location區段,這些區段客戶段不能訪問,只可以由內部產生的請
求來訪問,如try_files或error_page等
查找順序和優先級
1:帶有“=“的精確匹配優先
2:沒有修飾符的精確匹配
3:正則表達式按照他們在配置文件中定義的順序
4:帶有“^~”修飾符的,開頭匹配
5:帶有“~” 或“~*” 修飾符的,如果正則表達式與URI匹配
6:沒有修飾符的,如果指定字符串與URI開頭匹配
Location區段匹配示例location = / { # 只匹配 / 的查詢. [ configuration A ] } location / { # 匹配任何以 / 開始的查詢,但是正則表達式與一些較長的字符串將被首先匹配。 [ configuration B ] } location ^~ /images/ { # 匹配任何以 /images/ 開始的查詢并且停止搜索,不檢查正則表達式。 [ configuration C ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配任何以gif, jpg, or jpeg結尾的文件,但是所有 /images/ 目錄的請求將在Configuration C中處 理。 [ configuration D ] } 各 請求的處理如下例: ■/ → configuration A ■/documents/document.html → configuration B ■/images/1.gif → configuration C ■/documents/1.jpg → configuration D
root 、alias指令區別
location /img/ { alias /var/www/image/; }
#若按照上述配置的話,則訪問/img/目錄里面的文件時,ningx會自動去/var/www/image/目錄找文件
location /img/ { root /var/www/image; }
alias是一個目錄別名的定義,root則是最上層目錄的定義。
還有一個重要的區別是alias后面必須要用“/”結束,否則會找不到文件的。。。而root則可有可無~~
原文鏈接:https://www.cnblogs.com/xiaoliangup/p/9175932.html
相關推薦
- 2023-10-16 el-popover在原生table中,彈出多個以及內部取消按鈕無效問題
- 2022-05-11 JVM內存模型深度剖析與優化
- 2022-08-15 springboot的熱部署配置
- 2022-07-30 Golang?獲取系統信息的實現_Golang
- 2022-09-02 ORACLE正則匹配查詢LIKE查詢多個值檢索數據庫對象_oracle
- 2022-12-26 詳解C++11中的lambda匿名函數_C 語言
- 2022-12-29 Kotlin?Lambda表達式實踐使用介紹_Android
- 2022-04-02 nginx網頁緩存時間的配置過程_nginx
- 最近更新
-
- 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同步修改后的遠程分支