电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 电脑学院 > 服务器技术 > 正文
配置Apache为你的网站加速
配置Apache为你的网站加速
2009-10-29 12:00:06  文/天使别哭   出处:电脑软硬件应用网   

      如何加快自己的网站的速度,应该是各位站长、博主比较关心的问题。一个站点的打开速度关系到很多方面的内容,如服务器性能、网络速度及站点的网页设计等等。

      在Yahoo! Developer Network中的Best Practices for Speeding Up Your Web Site一文中就介绍了34种加速你的网站的建议。其中3项就涉及有关Web服务器的优化内容,这篇文章就是专门针对这3项建议来对Apache进行相应的设置,以达到网站加速的目的。


      为文件设置过期时间
      这样做的目的就是控制浏览器缓存我们不经常修改的文件,如图片、css、js等等,这样当浏览者打开另外一个网页的时候就减少了文件的下载,达到了加快网站速度的目的。要实现让Apache增加文件有效期让浏览器缓存可以用两种办法实现:

      第一种:利用mod_headers模块为文件增加有效期

      加载mod_headers模块,在配置文件中增加:

      LoadModule headers_module modules/mod_headers.so
配置mod_headers模块,设置jpg jpeg gif png ico js css swf flv等文件的Expires:(Expires头只能为固定的时间,形象点说就好像商品的过期时间。这样做的目的是为了兼容不支持Cache-Control的浏览器,如果设置了Cache-Control且浏览器支持Cache-Control的话就优先采用Cache-Control的设置,当然目前大部分浏览器支持Cache-Control文件头)

<FilesMatch "\.(jpg|jpeg|gif|png|ico|js|css|swf|flv)$">
        Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>
配置mod_headers模块,设置jpg jpeg gif png ico js css swf flv等文件的Cache-Control:(Cache-Control控制更为灵活一些,它设置的是一个有效时间,就好比商品的保质期,下面的604800单位为秒,就是设置文件的有效时间为为1星期)

<FilesMatch "\.(jpg|jpeg|gif|png|ico|swf|flv|js|css)$">
        Header set Cache-Control "max-age=604800"
</FilesMatch>
第二种:利用mod_expires模块为文件增加Expires

加载mod_expires模块,在配置文件中增加:

LoadModule expires_module modules/mod_expires.so
配置mod_expires模块:

ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 weeks"
ExpiresByType text/css access plus 3 days
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType text/plain "access plus 1 weeks"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType video/x-flv "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/html "access plus 1 weeks"
开启Gzip压缩
要让Apache实现Gzip压缩需要开启mod_deflate模块和mod_headers模块,前面已经开启了mod_headers模块,这里就再开启mod_deflate模块即可,打开Apache配置文件,添加:

LoadModule deflate_module modules/mod_deflate.so
配置mod_deflate模块,让其压缩html xml css php js等文件:


<ifmodule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-httpd-php application/x-javascript
  AddOutputFilter DEFLATE html xml css php js
</ifmodule>
关闭ETags
即使设置了Expires或者Cache-Control,有时浏览器在访问资源时往往会因为ETag的存在而重新下载整个资源,所以简单的做法是关闭它们:

FileETag None
相信设置好上面3项以后你的站点浏览速度会提高很多,如果自己感觉不到速度的提升,不妨安装Firefox的YSlow插件来检测一下你的设置是否成功。

  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    高手教你Apache服务器的配置与管理
    保障Web服务器 Apache安全五项措施
    Apache 2.x 服务器中的URL重写的配置…
    Windows 7 安装Apache服务失败怎么办
    Apache与IIS的优劣对比点点评
    Apache服务器如何穿越代理服务器
    Linux防火墙上的Apache反向代理
    Windows下的Apache服务器测试方法
    Apache HTTP 服务器配置
    Apache Indexes中文目录乱码解决办法
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号