Nginx静态资源站点——Nginx location作用
Nginx的locaiton作用是根據用戶請求的URI不同,來執行不同的應用。
針對用戶請求的網站URL進行匹配,匹配成功后進行對應的操作。
nginx.conf中server{}指令塊的location指令如下
location / {
root html;
index index.html index.htm;
}
location = /50x.html {
root html;
}
location [ = | ~| ~* | ^~ ] url {
#指定對應的動作
}
#正則表達式解釋
匹配符 匹配規則 優先級
= 精確匹配 1
^~ 以某個字符串開頭,不做正則 2
~* 正則匹配 3
/blog/ 匹配常規字符串,有正則就優先正則 4
/ 通用匹配,不符合其他location的默認匹配 5
1.修改nginx.conf如下
[root@web01 extra]# vim www.conf server {
listen 83;
server_name _;
#最低級匹配,不符合其他locaiton就來這
location / {
return 401; }
#優先級最高
location = / {
return 402; }
#以/blog/開頭的url,來這里,如符合其他locaiton,則以其他優先
location /blog/ {
return 403; }
#匹配任何以/img/開頭的請求,不匹配正則
location ^~ /img/ {
return 404; }
#匹配任何以.gif結尾的請求,支持正則
location ~* .(gif|jpg|jpeg)$ {
return 500; }
}
2.重載nginx
[root@web01 extra]# nginx -s reload
3.通過curl命令,檢測nginx的location匹配
curl命令
-s 不輸出錯誤和進度信息,靜默輸出
-o 輸出寫入到指定文件中 /dev/null 就是丟棄輸出,扔進黑洞
-I 只顯示響應頭
-w 完成后輸出哪些內容
#用戶請求的url為空或者有一個/
[root@bogon extra]# curl -s -o /dev/null -I -w “%{http_code}\n” 127.0.0.1:83 402
[root@bogon extra]# curl -s -o
/dev/null -I -w “%{http_code}\n” 127.0.0.1:83/ 402
#用戶請求存在index.html或者其他任意不符合其他location的字符串
[root@bogon extra]# curl -s -o /dev/null -I -w “%{http_code}\n” 127.0.0.1:83/index.html 401
#訪問以/blog/開頭的url
[root@bogon extra]# curl -s -o /dev/null -I -w “%{http_code}\n” 127.0.0.1:83/blog/index.html 403
#訪問以/img/開頭的url
[root@bogon extra]# curl -s -o /dev/null -I -w “%{http_code}\n” 127.0.0.1:83/img/2.gif 404
#匹配以gif結尾的url,由于/blog/優先級最低,因此匹配1.gif的規則
[root@bogon extra]# curl -s -o /dev/null -I -w “%{http_code}\n” 127.0.0.1:83/blog/1.gif 500
#由于/img/優先級更高,因此優先匹配
[root@bogon extra]# curl -s -o /dev/null -I -w “%{http_code}\n” 127.0.0.1:83/img/1.gif 404
總結
以上是生活随笔為你收集整理的Nginx静态资源站点——Nginx location作用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cad lisp 界址点号_CAD如何自
- 下一篇: Nginx-----相关配置-详细介绍