直播很久没有曝光量了,自荐一波《PHP进阶之路》(PHPer们,很久没有投资本身了呢?)
本文原文地址:https://mengkang.net/641.html
原创一篇博客不容易,请勿随意转载css
云时代,为了提高静态资源的加载速度,大伙都是拼了。这促使近些年国内 CDN 的使用逐步普及。而做为一家以图片分享社区为核心业务的公司,图片 CDN 的使用比较多,下面梳理下本身的一些经验。闭门造车,若有勘误,你们多多包涵。html
主要包括了如下内容:前端
由于下文中的CDN的使用都是基于咱们当前的图片存储,为了下文介绍不是那么突兀描述下当前图片存储的结构图:nginx
简单画了一张图予以说明:数据库
实际咱们在第五步,回源的时候,咱们会要求 CDN 服务商,不能全部节点直接回源到咱们源站,协商要求他们使用统一代理回源咱们源站,也就是说同一个资源只许他们回源一次。以后,其余边缘节点没有缓存,请求他们自身的代理。segmentfault
也就是说他们的 CDN 是有多级缓存的。浏览器
业务需求:如今须要将某个域名(a.mengkang.net)下的图片访问的流量切换到 CDN 上。缓存
操做步骤:服务器
先对原域名下访问日志作统计,统计出访问频次较高的图片地址(好比20万个地址),把这些地址交给cdn服务商。网络
把网宿 maa 的 cdn 切 400M 到蓝讯 mplus 上。
最初,咱们配置了n (好比 n=100)个二级域名,而后把这 n 个域名中的一半解析到网宿,另外一半解析到蓝讯。好比:
这里说的“切”是从咱们自身的业务代码动刀,而不是切换和 cdn 的域名解析合做。图片资源在数据库有全局惟一的uuid,而后根据该uuid生成url,只须要在这里控制url的域名输出便可。
关注源站的负载和 I/O
# top top - 16:25:34 up 273 days, 8:54, 1 user, load average: 5.54, 5.34, 5.19 Tasks: 209 total, 1 running, 208 sleeping, 0 stopped, 0 zombie Cpu(s): 0.2%us, 0.4%sy, 0.0%ni, 47.9%id, 51.0%wa, 0.0%hi, 0.5%si, 0.0%st Mem: 32945488k total, 32851176k used, 94312k free, 7033172k buffers Swap: 12485164k total, 204k used, 12484960k free, 22568232k cached
cdn 切换以后,除了观察新 cdn 的流量,还须要监控新 cdn 的缓存命中率,能够经过后期的回源比例来监控,也能够经过循环脚本执行wget来监测。
$ wget -S -O /dev/null f1.topitme.com/1/40/33/1110152459d0433401m.jpg --2015-12-29 11:11:01-- http://f1.topitme.com/1/40/33/1110152459d0433401m.jpg Resolving f1.topitme.com... 220.181.64.145 Connecting to f1.topitme.com|220.181.64.145|:80... connected. HTTP request sent, awaiting response... HTTP/1.1 200 OK Content-Type: image/jpeg Content-Length: 33061 Accept-Ranges: bytes Server: nginx Date: Mon, 30 Nov 2015 07:47:09 GMT Last-Modified: Mon, 08 Sep 2014 05:00:59 GMT Expires: Thu, 27 Nov 2025 07:47:09 GMT Cache-Control: max-age=315360000 Powered-By-ChinaCache: HIT from 010519g3H8.6 Age: 2489032 Powered-By-ChinaCache: HIT from 01001023P2.2 Switch:FSCS Length: 33061 (32K) [image/jpeg] Saving to: '/dev/null' /dev/null 100%[=====================>] 32.29K --.-KB/s in 0.09s 2015-12-29 11:11:02 (372 KB/s) - '/dev/null' saved [33061/33061]
这里出现了Powered-By-ChinaCache为HIT说明命中了,若是是MISS则没有命中,该规则须要咨询 cdn 运营商。
根据每一个产品的属性,每日流量确定都有高峰和低谷期,我以前设置的天然回源,虽然在流量高峰期,设置的比例很小,可是基数很大,因此切过去的量比较大,应该选择在流量比较少的时间,执行预加载,这样保证了带宽的使用率。
现象:同一图片地址,时而能打开,时而没法访问。没法访问时,单独访问图片地址发现还跳转到了一个游戏网站主页。
联系 CDN 客服,获得的反馈是运营商 DNS 劫持,他们的服务没问题。(很是的消极怠工)
拿下面这张图片做为例子 http://f4.topit.me/4/2d/d1/11...
首先咱们肯定咱们源站资源是可访问的,在 CDN 回源上不存在问题。
咱们经过wget命令绑定域名host,假如源站ip为111.1.23.214,这样则会绕过 CDN,直接访问咱们源站了。
wget -S -0 /dev/null --header="Host: f4:topit.me" http://111.1.23.214/4/2d/d1/1133196716aead12d4s.jpg
确认图片是能正常访问的。
而后经过wget -S
打印详细的 http 头信息
wget -S http://f4.topit.me/4/2d/d1/1133196716aead12d4s.jpg
请求结果以下
# wget -S http://f4.topit.me/4/2d/d1/1133196716aead12d4s.jpg --2014-11-08 21:47:34-- http://f4.topit.me/4/2d/d1/1133196716aead12d4s.jpg Resolving f4.topit.me... 123.150.50.14, 123.150.50.13 Connecting to f4.topit.me|123.150.50.14|:80... connected. HTTP request sent, awaiting response... HTTP/1.1 302 Moved Temporarily Server: nginx/1.7.3 Date: Sat, 08 Nov 2014 13:45:31 GMT Content-Type: text/html; charset=iso-8859-1 Content-Length: 218 Location: http://www.aiaigame.com/index.html Cache-Control: max-age=300 Expires: Sat, 08 Nov 2014 13:50:31 GMT Powered-By-ChinaCache: MISS from CHN-SX-3-3gC.2 Age: 125 Powered-By-ChinaCache: HIT from CHN-TJ-7-3V2.6 Connection: close Location: http://www.aiaigame.com/index.html [following] --2014-11-08 21:47:36-- http://www.aiaigame.com/index.html Resolving www.aiaigame.com... 119.90.14.54, 119.90.14.59, 220.181.64.153, ... Connecting to www.aiaigame.com|119.90.14.54|:80... connected. HTTP request sent, awaiting response... HTTP/1.1 200 OK Date: Sat, 08 Nov 2014 13:42:50 GMT Server: Apache/2.2.10 (Unix) DAV/2 PHP/5.2.6 mod_ssl/2.2.10 OpenSSL/0.9.8e-fips-rhel5 Last-Modified: Fri, 07 Nov 2014 09:14:50 GMT ETag: "31a8087-132ee-507413eb6f680" Accept-Ranges: bytes Content-Length: 78574 Cache-Control: max-age=300 Expires: Sat, 08 Nov 2014 13:47:50 GMT Vary: Accept-Encoding,User-Agent Content-Type: text/html Powered-By-ChinaCache: HIT from 01011623g3.3 Age: 288 Powered-By-ChinaCache: HIT from 01001743SJ Connection: keep-alive Length: 78574 (77K) [text/html] Saving to: “index.html.4” 100%[=====================================================================================================================================================>] 78,574 --.-K/s in 0.005s 2014-11-08 21:47:38 (16.3 MB/s) - “index.html.4” saved [78574/78574]
经过该请求,咱们能够清楚的看到,请求是先已经链接到了123.150.50.14:80而后发生的302跳转,头信息里清楚的写到Powered-By-ChinaCache: HIT from CHN-TJ-7-3V2.6,也就是说是 CDN 自身的问题,并且下面的跳转的网页也是使用ChinaCache的客户。
这样问题获得了定位,CDN 那边也没法再推脱,才着手处理。
访问网页时 css 里面的图片都没法访问,单独打开图片地址能访问。使用 wget --referer 定位是防盗链错误设置
故障截图(有点丑)
我把这个问题反馈给客服,给个人答复是他们没有作任何限制,是咱们源站的问题。那只能讲证据了。
referer
wget -S -O /dev/null --header="Host: img.topit.me" --referer="http://static.topitme.com/s/css/main21.css" http://211.155.84.132/img/bar/next.png
结果以下,说明源站是没有设置权限的
wget -S -O /dev/null --header="Host: img.topit.me" --referer="http://static.topitme.com/s/css/main21.css" http://211.155.84.132/img/bar/next.png --2015-05-07 13:52:50-- http://211.155.84.132/img/bar/next.png Connecting to 211.155.84.132:80... connected. HTTP request sent, awaiting response... HTTP/1.1 200 OK Server: nginx Date: Thu, 07 May 2015 05:52:50 GMT Content-Type: image/png Content-Length: 3022 Connection: keep-alive Last-Modified: Wed, 04 Jan 2012 14:44:07 GMT Expires: Sun, 04 May 2025 05:52:50 GMT Cache-Control: max-age=315360000 Accept-Ranges: bytes Length: 3022 (3.0K) [image/png]
同时,绑定host的也采用另外一种方式wget -e http_proxy
wget -SO /dev/null --referer="http://static.topitme.com/s/css/main21.css" http://img.topit.me/img/style/icon_heart.png -e http_proxy=211.155.84.137
wget -S -O /dev/null --referer="http://static.topitme.com/s/css/main21.css" http://img.topit.me/img/bar/next.png --2015-05-07 11:29:21-- http://img.topit.me/img/bar/next.png Resolving img.topit.me... 111.202.7.252, 125.39.78.164 Connecting to img.topit.me|111.202.7.252|:80... connected. HTTP request sent, awaiting response... HTTP/1.1 403 Forbidden Server: nginx Date: Thu, 07 May 2015 03:29:21 GMT Content-Type: text/html Content-Length: 162 Connection: keep-alive 2015-05-07 11:29:21 ERROR 403: Forbidden.
能够清晰的看到域名的解析过程,CDN DNS 经过预约义策略,返回到最优的 ip 111.202.7.252予以访问。而后返回了403。只有我截图对比了两种状况,CDN 客服才主动着手处理这个问题。
永远不要期望着客服来帮你解决问题,只有本身找到问题。