nginx 正則匹配與一般匹配競合


請看以下情境

......

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值