nginx之线上CDN的rewrite规则修改
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx之线上CDN的rewrite规则修改相关的知识,希望对你有一定的参考价值。
线上的CDN厂商的nginx的rewrite规则配置验证
验证测试:
为使nginx vhost虚拟主机配置文件更简洁,所以采用include方式,把nginx的rewrite规则写到一个单独的配置文件中
[[email protected] vhost]# grep include /usr/local/nginx/conf/vhost/img.test.conf
include /data/www/images/.htaccess;
下面的rewrite规则是云端CDN的提供的配置规则,但是其中2条规则存在问题
[[email protected] 03]# cat /data/www/images/.htaccess
rewrite ^/[a-zA-Z0-9]+/([0-9][0-9])([0-9][0-9])/([0-9]+).gjf$ /uploads/picture/2016/$1/$2/$3.gif last;
rewrite ^/[a-zA-Z0-9]+/([0-9][0-9])([0-9][0-9])/([0-9]+)(png|jpeg|jpg|gif)$ /uploads/picture/2016/$1/$2/$3.$4 last;
rewrite "^/.{6}[0-9]+/([0-9]{2})([0-9]{2})/(.*)$" /uploads/picture/2016/$1/$2/$3 ;
rewrite "^/.{6}[0-9]{4}/(.*)$" /uploads/picture/2016/$1 last;
rewrite "^/.{6}[0-9]{4}[0-9]{8}/(.*)$" /uploads/picture/$1 last; (此规则云端CDN配置有问题,位置应该放到第一的位置就可以了)
rewrite ^/[0-9]+.([0-9]+).([0-9]+).([0-9]+).(.*)$ /uploads/picture/$1/$2/$3/$4 last; (此规则云端CDN配置有问题,点号不需要转译的)
rewrite ^/[0-9]+/([0-9][0-9])([0-9][0-9])/(.*)$ /uploads/picture/2016/$1/$2/$3 last;
本人亲自测试成功的文件:
[[email protected] ~]# cat /data/www/images/.htaccess
##rewrite ^/[a-zA-Z0-9]+/(.*)$ /uploads/picture/$1 last;
rewrite "^/.{6}[0-9]{4}[0-9]{8}/(.*)$" /uploads/picture/$1 last;
rewrite ^/[a-zA-Z0-9]+/([0-9][0-9])([0-9][0-9])/([0-9]+).gjf$ /uploads/picture/2016/$1/$2/$3.gif last;
rewrite ^/[a-zA-Z0-9]+/([0-9][0-9])([0-9][0-9])/([0-9]+)(png|jpeg|jpg|gif)$ /uploads/picture/2016/$1/$2/$3.$4 last;
rewrite "^/.{6}[0-9]+/([0-9]{2})([0-9]{2})/(.*)$" /uploads/picture/2016/$1/$2/$3 ;
rewrite "^/.{6}[0-9]{4}/(.*)$" /uploads/picture/2016/$1 last;
##rewrite "^/.{6}[0-9]{4}[0-9]{8}/(.*)$" /uploads/picture/$1 last;
rewrite ^/[a-zA-Z0-9]+.([0-9]+).([0-9]+).([0-9]+).(.*)$ /uploads/picture/$1/$2/$3/$4 last;
rewrite ^/[0-9]+/([0-9][0-9])([0-9][0-9])/(.*)$ /uploads/picture/2016/$1/$2/$3 last;
nginx rewrite规则验证演示:
规则1
rewrite ^/[a-zA-Z0-9]+/([0-9][0-9])([0-9][0-9])/([0-9]+).gjf$ /uploads/picture/2016/$1/$2/$3.gif last;
说明:
/[a-zA-Z0-9]+/:匹配到任意的字符串;
$1指的是第一个圆括号([0-9][0-9]) 中的2位数;
$2指的是第二个圆括号([0-9][0-9]) 中的2位数;
$3指的是第三个圆括号([0-9]+)中的任意数字;([0-9]+).gjf:匹配到以gjf结尾的文件跳转到请求服务器上的([0-9]+).gif文件【注意:服务器上的([0-9]+).gif这个图片文件是必须存在的】
实例演示:
正常的浏览器访问:
http://img.test.com/uploads/picture/2016/08/03/1501742944.gif
http://img.test.com/uploads/picture/2016/08/03/1501742944.gif?base6412345
http://img.test.com/uploads/picture/2016/08/03/1501742944.gif???dwer
通过匹配到rewrite规则 浏览器访问:
http://img.test.com/awe21/0803/1501742944.gjf
http://img.test.com/22KDJH21/0803/1501742944.gjf??base64
服务器上文件的位置:
[[email protected] 03]# ll /data/www/images/uploads/picture/2016/08/03/1501742944.gif
-rw-r--r--. 1 root root 184440 Aug 3 2017 /data/www/images/uploads/picture/2016/08/03/1501742944.gif
规则2:
rewrite ^/[a-zA-Z0-9]+/([0-9][0-9])([0-9][0-9])/([0-9]+)(png|jpeg|jpg|gif)$ /uploads/picture/2016/$1/$2/$3.$4 last;
说明:
/[a-zA-Z0-9]+/:匹配到任意的字符串;
$1指的是第一个圆括号([0-9][0-9]) 中的2位数;
$2指的是第二个圆括号([0-9][0-9]) 中的2位数;
$3指的是第三个圆括号([0-9]+)中的任意数字【注意:这些数字必须是服务器上存在的并且以png|jpeg|jpg|gif结尾文件的前面的数字】;
$4指的是第四个括号(png|jpeg|jpg|gif)中的服务器上必须存在的以png|jpeg|jpg|gif结尾的文件
实例演示:
http://img.test.com/uploads/picture/2016/07/28/1469696883.jpeg
http://img.test.com/uploads/picture/2016/07/28/1469696883.jpeg??base64
http://img.test.com/aA01/0728/1469696883jpeg??base64
http://img.test.com/1/0728/1469696883jpeg
http://img.test.com/34chk/0728/1469696883jpeg
http://img.test.com/chkDHK/0728/1469696883jpeg
http://img.test.com/chkDHK123654/0728/1469696883jpeg?2345
规则3:
rewrite "^/.{6}[0-9]+/([0-9]{2})([0-9]{2})/(.*)$" /uploads/picture/2016/$1/$2/$3 ;
说明:
^/.{6}[0-9]+/:匹配到任意6个字符串加任意数字;
$1指的是第一个圆括号([0-9]{2}) 中的2位数;
$2指的是第二个圆括号([0-9]{2}) 中的2位数;
$3指的是第三个圆括号(.*)中的任意字符串。当然$1,$2,$3这些字符串必须是服务器上实实在在存在的字符串,只有这样在浏览器请求时,才能获取到服务器上的图片
实例演示:
http://img.test.com/uploads/picture/2016/07/28/1469696883.jpeg
http://img.test.com/cHrtwK123/0728/1469696883.jpeg
http://img.test.com/cHrtwK1234/0728/1469696883.jpeg
http://img.test.com/cHrtwK4/0728/1469696883.jpeg??base64
规则4:
rewrite "^/.{6}[0-9]{4}/(.*)$" /uploads/picture/2016/$1 last;
说明:
^/.{6}[0-9]{4}:匹配到任意6个字符串加任意4个数字
实例演示:
http://img.test.com/uploads/picture/2016/07/28/1469696883.jpeg
http://img.test.com/cHrtwK4123/07/28/1469696883.jpeg
http://img.test.com/cH23wK4155/07/28/1469696883.jpeg
以下这样的是不符合规则的,所以找不到文件
http://img.test.com/cH23wK41/07/28/1469696883.jpeg
规则5:
rewrite "^/.{6}[0-9]{4}[0-9]{8}/(.*)$" /uploads/picture/$1 last;
说明:
^/.{6}[0-9]{4}[0-9]{8} :匹配到任意6个字符串加任意4个数字再加8个任意的数字
实例演示:
http://img.test.com/uploads/picture/2016/07/28/1469696883.jpeg
经测试,此rewrite规则放到此位置,是匹配不到图片的,所以位置得变动下,把此规则放到规则的首位就可以了
http://img.test.com/uploads/picture/2016/07/28/1469696883.jpeg
http://img.test.com/weijdg444433336666/2016/07/28/1469696883.jpeg
规则6:
rewrite ^/[a-zA-Z0-9]+.([0-9]+).([0-9]+).([0-9]+).(.*)$ /uploads/picture/$1/$2/$3/$4 last;
注意:此处的点号是不需要转译的,转译会导致rewrite规则不可用,就如下面的这条rewrite规则是不正确的
rewrite ^/[0-9]+.([0-9]+).([0-9]+).([0-9]+).(.*)$ /uploads/picture/$1/$2/$3/$4 last;
实例演示:
http://img.test.com/uploads/picture/2018/08/18/1489719802.png?base64
http://img.test.com/1wer1.2018.08.18/1489719802.png
http://img.test.com/SHDw.2018.08.18/1489719802.png?base64
http://img.test.com/SHDw.2018.08.18/1489719802.png?base641234
规则7:
rewrite ^/[0-9]+/([0-9][0-9])([0-9][0-9])/(.*)$ /uploads/picture/2016/$1/$2/$3 last;
实例演示:
http://img.test.com/uploads/picture/2016/07/28/1469696883.jpeg
http://img.test.com/1232345/0728/1469696883.jpeg?
http://img.test.com/1245/0728/1469696883.jpeg?1234
总结:
Apache和nginx的rewrite规则的匹配是有顺序的,而且是从上往下依次匹配的。如果上面优先被匹配到就不再匹配下面的规则。
以上是关于nginx之线上CDN的rewrite规则修改的主要内容,如果未能解决你的问题,请参考以下文章