nginx location匹配規則
nginx location匹配規則
語法規則
location [=|~|~*|^~|!~|!~*] /pattern/{...}
預設值:no
使用欄位:server,location
修飾符 | 功能 |
---|---|
= | 精確匹配 |
~ | 正則表示式模式匹配,區分大小寫, !~取反 |
~* | 正則表示式模式匹配,不區分大小寫, !~*取反 |
^~ | 字首匹配,類似於無修飾符的行為,也是以指定模組開始,不同的是,如果模式匹配,那麼就停止搜尋其他模式了,不支援正則表示式 |
@ | 定義命名location區段,這些區段客戶端不能訪問,只可以由內部產生的請求來訪問,如try_files或error_page等 |
為某個請求URI(路徑)建立配置。
路徑匹配在URI規範化以後進行。所謂規範化,就是先將URI中形如“%XX
”的編碼字元進行解碼, 再解析URI中的相對路徑“.
”和“..
”部分, 另外還可能會壓縮相鄰的兩個或多個斜線成為一個斜線。
可以使用字首字串或者正則表示式定義路徑。使用正則表示式需要在路徑開始新增“~*
”字首 (不區分大小寫),或者“~
”字首(區分大小寫)。為了根據請求URI查詢路徑,nginx先檢查字首字串定義的路徑 (字首路徑),在這些路徑中找到能最精確匹配請求URI的路徑。然後nginx按在配置檔案中的出現順序檢查正則表示式路徑, 匹配上某個路徑後即停止匹配並使用該路徑的配置,否則使用最大字首匹配的路徑的配置。
路徑可以巢狀,但有例外。
如果最大字首匹配的路徑以“^~
”開始,那麼nginx不再檢查正則表示式。
而且,使用“=
”字首可以定義URI和路徑的精確匹配。如果發現匹配,則終止路徑查詢。 比如,如果請求“/
”出現頻繁,定義“location = /
”可以提高這些請求的處理速度, 因為查詢過程在第一次比較以後即結束。這樣的路徑明顯不可能包含巢狀路徑。
官方示例:
location = / { [ configuration A ] } location / { [ configuration B ] } location /documents/ { [ configuration C ] } location ^~ /images/ { [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] }
請求“/
”匹配配置A, 請求“/index.html
”匹配配置B, 請求“/documents/document.html
”匹配配置C, 請求“/images/1.gif
”匹配配置D, 請求“/documents/1.jpg
”匹配配置E。
字首“@
”定義了命名路徑。這種路徑不在一般的請求處理中使用, 而是用在請求重定向中。這些路徑不能巢狀,也不能包含巢狀路徑。
匹配優先順序
1、= 精確匹配
嚴格匹配,只有完全相等才匹配成功,然後停止匹配,不然繼續匹配
2、^~ 字串字首匹配
字串字首匹配,字首字串相同就匹配成功,然後停止匹配,不然繼續匹配
如果有包含關係,按最大匹配原則匹配,比如在字首匹配:location /dir01
與 location /dir01/dir02
,如有請求 http://localhost/dir01/dir02/file
將最終匹配到 location /dir01/dir02
3、正則匹配 ~ 和 ~*
~ 和 ~* 實質上是同級的,優先順序取決於配置檔案書寫的先後順序,先寫的優先順序高
~ 區分大小寫的匹配
進行區分大小寫的正則匹配,匹配成功後仍續搜尋,如果有多個匹配,取最長匹配
~ 區分大小寫的匹配*
進行不區分大小寫的正則匹配,匹配成功後仍續搜尋,如果有多個匹配,取最長匹配
4、 通用匹配 location / {......}
所有請求都可以匹配到,匹配到一個普通格式後,搜尋並未結束,而是暫存當前匹配的結果,並繼續搜尋正則匹配模式 ,匹配到正則就交給正則匹配處理,如果沒有匹配到正則就以暫存的結果為匹配結果
優先順序次序如下:
( location = 路徑 )
--> ( location ^~ 路徑 )
--> ( location ~ 正則 ) 、( location ~* 正則 )
--> ( location 路徑 )
匹配例項
安裝echo模組用於除錯
#下載模組
[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
#解壓
[root@localhost ~]# tar xf echo-nginx-module-0.61
#檢視原來的引數,預編譯時加上--add-module=/root/echo-nginx-module-0.61 ,新增echo模組
[root@localhost ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=/root/echo-nginx-module-0.61
[root@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=/root/echo-nginx-module-0.61
[root@localhost nginx-1.18.0]# make
[root@localhost nginx-1.18.0]# cd objs/
[root@localhost objs]# ls
addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
autoconf.err nginx ngx_auto_config.h ngx_modules.c src
#備份正在在使用的nginx
[root@localhost ~]# cp /usr/local/nginx/sbin/nginx /opt/
#新編譯的nginx替換原來的
[root@localhost objs]# nginx -s stop;cp nginx /usr/local/nginx/sbin/;nginx
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y
1. 無修飾符
#任何未匹配到其它location的請求都會匹配到
server {
listen 80;
server_name www.test.com;
location /abc {
echo "無修飾符(/)";
}
......
}
[root@localhost ~]# curl www.test.com/abc
無修飾符(/)
[root@localhost ~]# curl www.test.com/abc/
無修飾符(/)
[root@localhost ~]# curl www.test.com/abc/ABc
無修飾符(/)
[root@localhost ~]# curl www.test.com/abc/abc
無修飾符(/)
[root@localhost ~]# curl www.test.com/abc/ABc/
無修飾符(/)
2. = 對普通字元精確匹配
location = /uri = 開頭表示精確匹配,只有完全匹配上才能生效。
server {
listen 80;
server_name www.test.com;
location /abc {
echo "無修飾符(/)";
}
location = /abc {
echo "精確匹配(=)";
}
}
[root@localhost ~]# curl www.test.com/abc
精確匹配(=)
[root@localhost ~]# curl www.test.com/abc/
無修飾符(/)
[root@localhost ~]# curl www.test.com/abc/ABC
無修飾符(/)
[root@localhost ~]# curl www.test.com/abc?a=1\$b=2
精確匹配(=)
3. ^~ 匹配字串開頭
server {
listen 80;
server_name www.test.com;
location ^~ /static {
echo "匹配開頭(^~)";
}
}
[root@localhost ~]# curl www.test.com/static
匹配開頭(^~)
[root@localhost ~]# curl www.test.com/staticcc
匹配開頭(^~)
[root@localhost ~]# curl www.test.com/static/
匹配開頭(^~)
4. ~ 區分大小寫的匹配
server {
listen 80;
server_name www.test.com;
location ~ /images/ {
echo "區分大小寫的匹配(~)";
}
}
[root@localhost ~]# curl www.test.com/IMAGES/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
[root@localhost ~]# curl www.test.com/images/
區分大小寫的匹配(~)
[root@localhost ~]# curl www.test.com/images/1.jpg
區分大小寫的匹配(~)
5. ~* 不區分大小寫的匹配
server {
listen 80;
server_name www.test.com;
location ~* \.(gif|jpg|jpeg)$ {
echo "不區分大小寫的匹配(~*)";
}
}
[root@localhost ~]# curl www.test.com/a.jpg
不區分大小寫的匹配(~*)
[root@localhost ~]# curl www.test.com/A.jpg
不區分大小寫的匹配(~*)
[root@localhost ~]# curl www.test.com/a.jpg/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
訪問控制
location段
allow:設定允許哪臺或哪些主機訪問,多個引數間用空格隔開
deny:設定禁止哪臺或哪些主機訪問,多個引數間用空格隔開
示例:
allow 192.168.1.1/32 172.16.0.0/16;
deny all;
要點總結:
location 的匹配順序是“先匹配普通,再匹配正則”
普通匹配與配置書寫順序無關,因為按照匹配的長短來取匹配結果
正則匹配與順序有關,因為是從上往下匹配。
正則匹配項匹配規則,受配置檔案的前後順序影響,但普通匹配模式不會