請看以下情境
......
location / { #一般匹配
root /use/local/nginx/html;
index index.html index.htm;
}
location ~ image { # 正則匹配
root /var/www/;
root index.html;
}
......
問題:
若訪問 http://xx.com/image/cover.png
此時固然 / 與 /image/cover.png 匹配成功
但正則 /image 與 /image/cover.png 似亦能匹配
則何者將發揮作用?
實驗開始:
1. 創建正則專用的圖片
# mkdir /var/www/image
進入該資料夾
# cd /var/ww/image
取得圖片
# wget [隨便一個圖片網址]
將取得的圖片改名
# mv [下載圖片名] cover.png
2. 創建一般匹配用的圖片
# mkdir /usr/local/nginx/html/image
# cd /usr/local/nginx/html/image
# wget [隨便一個圖片網址 但不要跟 1.的圖片相同]
將取得的圖片改名
# mv [下載圖片名] cover.png
3. 編輯index.html
# vim /usr/local/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<img src="./image/cover.png"/><!-- 加上這行觀察 -->
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
<!-- <script>
window.location.href ='/';
</script> -->
</html>
重啟後生效
# ./usr/local/nginx/sbin/nginx -s reload
結論: 出現/var/www/image/cover.png, 代表正則發揮作用(會先去執行一般匹配, 再執行正則匹配 即後蓋前)
如有錯誤發生 請觀察日誌
# tail /usr/local/nginx/loga/error.log