您的位置:首页 > 运维架构 > Apache

apache配置优化(六)

2013-10-21 13:50 375 查看
Apache--mod_expires缓存模块(这是apache调优的重要参数)mod_expires介绍:mod_expires允许图片在用户浏览器进行缓存,用户打开网站,第一次会把图片加载到本地,如果在有效期呢,用户在访问,就不需要去网站读取,只是加载在本地。好处一:提升用户体验由于用户读本地缓存了,所以访问页面就快了,也节省网站的带宽流量成本。好处二:节省网站带宽成本由于读本地缓存了,和服务器的交互就少了,也节省网站带宽流量成本。好处三:节省网站服务器及维护成本由于用户读本地缓存了,和服务器的交互就少了,服务器的压力小了,服务器数量及维护人员等成本都降低了,这也是为什么taobao的缓存是10年过期。expires失效条件:1、内容过期2、用户自己手动清除控制expires方法:如果网站更新功能或更新文件后,用户再访问时的内容还是旧的(上面已提到不会再下载了)怎么解决这个问题呢?1)首先,对于大公司业务来说,图片等资源一般很少会去修改。因此,taobao,jd可以肆无忌惮的吧expirts设置为10年。一年可以节省费用上亿元。2)对于js,css偶尔会变化的资源,一般expires设置时间会比较短。如1-30天。3)在更新文件上采取策略,如,更新后以新的文件名发布,这样对于用户又是新的资源了。expires特殊缓存情况:1)特殊缓存,google首页expires一日(经常会变更图片)2)网站的js统计代码不会设置缓存
检查模块是否安装:[root@eric6 ~]#/application/apache/bin/apachectl -l|grep mod_expires.cmod_expires.c[root@eric6 ~]#/application/apache/bin/apachectl -M|grep expiresexpires_module (static)Syntax OK如果有以上两个参数,说明mod_expires已经编译安装如果在查找的时候没有安装该模块,可以使用DSO方式安装:[root@eric6metadata]# cd /home/tools/httpd-2.2.25/modules/metadata/[root@eric6metadata]# ll mod_expires.c-rw-r--r-- 1liuyalei liuyalei 18285 11月 12 2008mod_expires.c[root@eric6 metadata]#/application/apache/bin/apxs -c -i -a mod_expires.c
[root@eric6metadata]# wget 127.0.0.1--2013-10-2022:12:10--http://127.0.0.1/Connecting to127.0.0.1:80... connected.HTTP request sent,awaiting response... 200 OKLength: 8[text/html]Saving to:`index.html.1'
100%[==========================================================================================>]8--.-K/sin 0s
2013-10-20 22:12:10(1.77 MB/s) - `index.html.1' saved [8/8]
[root@eric6metadata]# curl -I 127.0.0.1HTTP/1.1 200 OKDate: Sun, 20 Oct2013 14:12:11 GMTServer:Apache/2.2.25 (Unix) DAV/2Last-Modified: Sat,19 Oct 2013 04:57:07 GMTETag:"43ddb-8-4e910e1fef476"Accept-Ranges:bytesContent-Length: 8Content-Type:text/html注释expires配置ExpiresActive on#开启expires功能ExpiresDefault "access plus 12month"#缺省缓存情况(12个月)ExpiresByType text/html "accessplus 12 months"#缓存类型htmlExpiresByType text/css "accessplus 12 months"#缓存类型cssExpiresByType image/gif "accessplus 12 months"#缓存类型gifExpiresByType image/jpeg "accessplus 12 months"#缓存类型jpegExpiresByType image/jpg "accessplus 12 months"#缓存类型jpgExpiresByType image/png "accessplus 12 months"#缓存类型pngEXpiresByTypeapplication/x-shockwave-flash "access plus 12 months"#缓存类型flashEXpiresByType application/x-javascript"access plus 12 months"#缓存类型JavaScriptExpiresByType video/x-flv "accessplus 12 months"#缓存类型x-flv实例:为一个server标签添加expires功能[root@eric6apache]# vi /application/apache/conf/extra/httpd-vhosts.conf<VirtualHost*:80>ServerAdmin 952773925@qq.comDocumentRoot "/var/bbs"ServerName bbs.liuyalei.comServerAlias liuyalei.comErrorLog "logs/bbs-error_log"CustomLog "logs/bbs-access_log"commonExpiresActive onExpiresDefault "access plus 12month"ExpiresByType text/html "accessplus 12 months"ExpiresByType text/css "accessplus 12 months"ExpiresByType image/gif "accessplus 12 months"ExpiresByType image/jpeg "accessplus 12 months"ExpiresByType image/jpg "accessplus 12 months"ExpiresByType image/png "accessplus 12 months"EXpiresByTypeapplication/x-shockwave-flash "access plus 12 months"EXpiresByType application/x-javascript"access plus 12 months"ExpiresByType video/x-flv "accessplus 12 months"</VirtualHost>[root@eric6apache]# ../apache/bin/apachectl -tSyntax OK[root@eric6apache]# ../apache/bin/apachectl graceful[root@eric6apache]# ps -ef|grep httpdroot473410 11:56 ?00:00:03/application/apache2.2.25/bin/httpd -k startdaemon817747340 22:30 ?00:00:00/application/apache2.2.25/bin/httpd -k startdaemon817847340 22:30 ?00:00:00/application/apache2.2.25/bin/httpd -k startdaemon817947340 22:30 ?00:00:00/application/apache2.2.25/bin/httpd -k startdaemon818047340 22:30 ?00:00:00/application/apache2.2.25/bin/httpd -k startroot826379690 22:30 pts/000:00:00 grep httpd服务端网站上传一张图片,然后在火狐浏览器安装2个插件,进行查看,检查缓存情况:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  apache配置优化