magento性能优化系列一:.htaccess调整及其他

来源:互联网 发布:手机淘宝beta版已过期 编辑:程序博客网 时间:2024/06/06 00:39

本文主要讲解magento性能优化的几个方面。如果不加注明的话,基本上适用任一版本。

调整 .htaccess

默认的 .htaccess是包含有关于处理性能的部分的,但是是被注释掉的,可以选择合适的部分取消注释;

启用输出压缩

这一部分会打开 apache 的mod_deflate模块,将text、 css 和 javascript 先进行压缩再发送到浏览器。这样就会减少网络下载量,缩短等待时间,示例如下:

[plain] view plaincopy
  1. <IfModule mod_deflate.c>  
  2.   
  3. ############################################  
  4. ## enable apache served files compression  
  5. ## http://developer.yahoo.com/performance/rules.html#gzip  
  6.   
  7.     # Insert filter on all content  
  8.     SetOutputFilter DEFLATE  
  9.     # Insert filter on selected content types only  
  10.     AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript   
  11.   
  12.     # Netscape 4.x has some problems...  
  13.     BrowserMatch ^Mozilla/4 gzip-only-text/html  
  14.   
  15.     # Netscape 4.06-4.08 have some more problems  
  16.     BrowserMatch ^Mozilla/4\.0[678] no-gzip  
  17.   
  18.     # MSIE masquerades as Netscape, but it is fine  
  19.     BrowserMatch \bMSIE !no-gzip !gzip-only-text/html  
  20.   
  21.     # Don't compress images  
  22.     SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary  
  23.   
  24.     # Make sure proxies don't deliver the wrong content  
  25.     Header append Vary User-Agent env=!dont-vary  
  26.   
  27.     # enable resulting html compression  
  28.     php_flag zlib.output_compression on  
  29.  </IfModule>  

启用Expires Headers

注意:这一配置在Litespeed servers无效
浏览器使用 Expires Headers来确定页面组件可以被缓存多长时间。静态的部件,如图像,可以设置为永不过期,但还是建议设置一个Expires Headers。要打开这个特性,可以取消注释对应的行并添加"ExpiresActive On" 如下所示: 

[plain] view plaincopy
  1. <IfModule mod_expires.c>  
  2.   
  3. ############################################  
  4. ## Add default Expires header  
  5. ## http://developer.yahoo.com/performance/rules.html#expires  
  6.   
  7.     ExpiresActive On  
  8.     ExpiresDefault "access plus 1 year"  
  9.   
  10. </IfModule>  

禁用 ETags
ETags(Entity tags)是服务器和浏览器的一个功能,它用来判断浏览器缓存里的元素是否和原来服务器上的一致。ETags比last-modified date更具有弹性,它用一个独一无二的字符串来标识一个元素的版本。 要关闭它很简单,做法如下:

[sql] view plaincopy
  1. ############################################  
  2. ## If running in cluster environment, uncomment this  
  3. ## http://developer.yahoo.com/performance/rules.html#etags  
  4.   
  5.     FileETag none  

Magento 后台设置及调整

有几个功能,可以在 Magento 的后台启用他们。
合并CSS 和JS 文件

合并之后,能有效减少http请求数量,1.4.x或之前的版本,可以使用Fooman_Speedster 扩展替代此设置。
  1:  登入后台 System > Configuration > Developer.
  2:  "Javascript Settings", 设置 "Merge Javascript Files" 为 YES.
  3: 在"CSS Settings", 设置"Merge CSS Files" 为 YES.
  4: Clear the cache.

启用 Magento 编译特性

magento程序文件存放在如下目录

[html] view plaincopy
  1. app/code/local  
  2. app/code/community  
  3. app/code/core  
  4. lib  

每次加载页面时,都需要搜索这些文件,并读取对应文件;Mage_Compiler 能有效减少对这些文件的重复搜索和读取,因为它将所有的application文件都copy到一个单一的目录里,并且会缓存常用的页面。
    1:登入后台 System > Tools > Compilation.
    2:Click "Run Compilation Process"

其他调整

使用并行下载;大多数浏览器都会限制对域最大连接数位4。如果您的页面有很多来自相同的域中的组件,这会导致花费较长的时间加载页面。但是你可以将一些静态内容映射到不同的子域上,从而欺骗浏览器同时运行更多个进程。
1:在服务器上创建 Pointer domains . 如 "static.example.com",  "js.example.com", "media.example.com", "skin.example.com" 。
2.改变 media, js, 和skin URLs 在Magento后台,如图所示:

3:编辑Vhost file使apache不要尝试加载子目录../media, ../skin, or ../js,而是加载根目录,示例如下:

  # subdomain logic
  RewriteEngine On
  RewriteOptions inherit
  RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
  RewriteCond %{HTTP_HOST} !^example\.com [NC]

Add:

  RewriteCond %{HTTP_HOST} !^js\.example\.com [NC]
  RewriteCond %{HTTP_HOST} !^media\.example\.com [NC]
  RewriteCond %{HTTP_HOST} !^skin\.example\.com [NC]

4:Save the file and restart Apache.
5:Clear the cache and reload the page 并确保静态内容是来自于新的URLs.

6:Clean cache

清除DB相关log


0 0