码迷,mamicode.com
首页 > 其他好文 > 详细

nginx之线上CDN的rewrite规则修改

时间:2018-08-12 18:50:30      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:测试   rewrite   data   个数   gif   服务   依次   找不到文件   虚拟主机   

线上的CDN厂商的nginx的rewrite规则配置验证

验证测试:
为使nginx vhost虚拟主机配置文件更简洁,所以采用include方式,把nginx的rewrite规则写到一个单独的配置文件中

[root@test01 vhost]# grep include /usr/local/nginx/conf/vhost/img.test.conf 
   include /data/www/images/.htaccess; 

下面的rewrite规则是云端CDN的提供的配置规则,但是其中2条规则存在问题

[root@test01 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;

本人亲自测试成功的文件:

[root@test01 ~]# 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

服务器上文件的位置:

[root@test01 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规则修改

标签:测试   rewrite   data   个数   gif   服务   依次   找不到文件   虚拟主机   

原文地址:http://blog.51cto.com/wujianwei/2158489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!