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;
用戶認證
auth_basic "歡迎信息";
auth_basic_user_file "/path/to/user_auth_file"
user_auth_file內容格式為:
username:password
這里的密碼為加密后的密碼串,建議用htpasswd來創建此文件:
htpasswd -c -m /path/to/.user_auth_file USERNAME
要點總結:
location 的匹配順序是“先匹配普通,再匹配正則”
普通匹配與配置書寫順序無關,因為按照匹配的長短來取匹配結果
正則匹配與順序有關,因為是從上往下匹配。
正則匹配項匹配規則,受配置文件的前后順序影響,但普通匹配模式不會