thinkphp5.1使用cdn加速-miss缓存未命中解决
在单独给首页配置cdn加速后,出现了x-cache: miss tcp_miss dirn:-2:-2,miss也就是缓存没有被命中。这个问题不是配置错了,而框架问题。
什么要使用cdn?
“cdn简介 cdn的全称是content delivery network,即内容分发网络。
为什么使用cdn?
cdn就近节点访问,提高了网站加载速度,降低延时;能起到分流作用,减轻服务器负载,
使用动静分离提高网站访问速度。
配置实现
这个呢,我是部署在了生产环境,实际测试大并发高流量处理,当然了,也有很多的不足。
环境
服务器:腾讯云
系统:centos7
框架:thinkphp5.1
使用阿里云oss+cdn并配置缓存
当然还要有经过备案的域名。
关于怎么使用阿里云oss对象存储系统,laravel6.x上传视频到阿里云oss对象存储 这篇文章记录过。
bucket创建完成后,可以在列表中看到 '阿里云 cdn 加速',点击这个进行相关的cdn操作,配置完后需要在域名中进行解析。
问题
在单独给首页配置cdn加速后,出现了x-cache: miss tcp_miss dirn:-2:-2,miss也就是缓存没有被命中。这个问题不是配置错了,而框架问题。
解决方法如下:
第一步:public/index.php读取/设置缓存限制器
<?phpnamespace think;// 设置缓存限制session_cache_limiter('public');require __dir__ . '/../thinkphp/base.php';container::get('app')->run()->send();
第二步:开启请求缓存
// 文件位置: config/app.php'request_cache' => true,
ab压测
如下压测是经过了cdn+oss之后的结果,可以看到qps可以达到573
$ ab -c100 -n1000 你的页面urlthis is apachebench, version 2.3 <$revision: 1430300 $>copyright 1996 adam twiss, zeus technology ltd, http://www.zeustech.net/licensed to the apache software foundation, http://www.apache.org/benchmarking miaosha.nikexu.com (be patient)completed 100 requestscompleted 200 requestscompleted 300 requestscompleted 400 requestscompleted 500 requestscompleted 600 requestscompleted 700 requestscompleted 800 requestscompleted 900 requestscompleted 1000 requestsfinished 1000 requests server software: tengineserver hostname: miaosha.nikexu.comserver port: 80document path: /document length: 774 bytesconcurrency level: 100time taken for tests: 1.745 secondscomplete requests: 1000failed requests: 0write errors: 0total transferred: 1403829 byteshtml transferred: 774000 bytesrequests per second: 573.16 [#/sec] (mean)time per request: 174.472 [ms] (mean)time per request: 1.745 [ms] (mean, across all concurrent requests)transfer rate: 785.76 [kbytes/sec] receivedconnection times (ms) min mean[+/-sd] median maxconnect: 15 17 1.5 16 25processing: 18 91 113.4 71 1195waiting: 18 90 113.4 71 1195total: 36 107 113.4 87 1212percentage of the requests served within a certain time (ms) 50% 87 66% 99 75% 103 80% 105 90% 120 95% 195 98% 437 99% 834100% 1212 (longest request)
在来看看500个并发的压测。
ab -c500 -n10000 你的页面urlrequests per second: 965.61 [#/sec] (mean)time per request: 517.806 [ms] (mean)time per request: 1.036 [ms] (mean, across all concurrent requests)percentage of the requests served within a certain time (ms) 50% 257 66% 319 75% 541 80% 1043 90% 1102 95% 1281 98% 1560 99% 1821100% 3771 (longest request)
通过这个两个手段,已经可以看到还并发量提供,500的并发量,80%的用户可以在1s左右打开。在没有使用cdn+oss前,我压测的结果是 150的并发,cpu就冲到了90%。使用oss+cdn的优势就不言而喻了。
推荐学习:《thinkphp视频教程》
以上就是快速解决thinkphp5.1出现miss缓存未命中问题的详细内容。
