zoukankan      html  css  js  c++  java
  • IIS或Apache启用GZIP压缩优化网站

    先来了解一下GZIP,gzip 是GNU zip的缩写,它是一个GNU自由软件的文件压缩程序,也经常用来表示gzip这种文件格式。软件的作者是Jean-loup Gailly和Mark Adler。1992年10月31日第一次公开发布,版本号是0.1,目前的稳定版本是1.2.4。

    gzip可以级大的加速网站.有时压缩比率高到80%,近来测试了一下,最少都有40%以上,还是相当不错的.在Apache2之后的版本,模块名不叫gzip,而叫mod_deflate

    1、Apache启用gzip

    如果要开启gzip的话,一定要打开下面二个模块.
    LoadModule headers_module modules/mod_headers.so
    LoadModule deflate_module modules/mod_deflate.so

    设置压缩比率,取值范围在 1(最低) 到 9(最高)之间,不建议设置太高,虽然有很高的压缩率,但是占用更多的CPU资源.
    DeflateCompressionLevel 3
    AddOutputFilter DEFLATE html xml php js css
    <Location />
    SetOutputFilter DEFLATE
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    SetEnvIfNoCase Request_URI \\.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary

    Header append Vary User-Agent env=!dont-vary #对代理的设置
    </Location>

    下面二个测试网站

    http://www.whatsmyip.org/mod_gzip_test/

    http://www.gidnetwork.com/tools/gzip-test.php

    测试数据对css
    Original Size: 44 KB
    Gzipped Size: 10 KB
    Data Savings: 77.27%

    测试数据js
    Original Size: 6 KB
    Gzipped Size: 2 KB
    Data Savings: 66.67%

    测试数据php
    Original Size: 62 KB
    Gzipped Size: 15 KB
    Data Savings: 75.81%

    上面只是随机拿的几个数据,看的出来,使用了gzip压缩后文件小多了.

    另外讲一下,有关squid对gzip的处理
    在squid中,对同一个URL只保留一份缓存。对于如果不同browser(是否支持压缩)如果频繁交替访问,例如:对某个cache住的目标,一个 http/1.0请求可能会导致squid强制更新其缓存。但接下来的另一个http/1.1请求又会导致squid再次更新缓存。这样那squid缓存 数据就要频繁更新,这就极大的降低了cache命中率。
    不过还好,现实环境中不支持压缩的browser毕竟是很少的情况,所以对于缓存命中率的降低很有限.

    这个神通广大的模块就是mod_gzip. 它通过用和gzip一样的压缩算法对apache发出的页面进行压缩,可能的话可以把页面压缩成为原来大小的十份之一。哪,如果10K的页面只要传1K这 不就是提速10倍嘛。当然一般网页只可以达到3-6倍。那也很不错。对吧。连google这样一个大的网站都采用这个技术。你还不快跟上?

    这样一个好东东,来来来,我告诉你如何安装:分3步,1、下载,2、修改配置,3、测试。

    下载

    http://www.remotecommunications.com/apache/mod_gzip 下载mod_gzip.c 还有它的补丁。

    安装, 配置
    把mod_gzip放到你的apache的源代码目录下,新建一个mod_gzip目录如果需要补丁(针对1.3.17.la版) 还需运行:
    patch mod_gizp.c
    按你需要,在配置中选择动态DSO或静态编译进apache系统。如何处理在README中讲得很清楚,如-add- module=mod_gzip.c, make,make install等等。这里不多讲。

    把下列配置加入httpd.conf尾部。
    # MOD_GZIP configuration
    mod_gzip_on Yes
    mod_gzip_minimum_file_size 1002
    mod_gzip_maximum_file_size 0
    mod_gzip_maximum_inmem_size 60000
    mod_gzip_item_include mime "application/x-httpd-php"
    mod_gzip_item_include mime text/*
    mod_gzip_item_include mime "httpd/unix-directory"
    mod_gzip_dechunk Yes
    mod_gzip_temp_dir "/tmp"
    mod_gzip_keep_workfiles No
    mod_gzip_item_include file ".php3$"
    mod_gzip_item_include file ".txt$"
    mod_gzip_item_include file ".html$"
    mod_gzip_item_exclude file ".css$"
    mod_gzip_item_exclude file ".js$"

    在保存修改后运行
    …/bin/apachectl configtest确保配置修改无误。
    然后用 apachectl restart 指令重起服务。

    修改,测试

    在宣布做好了之前在测试一下是优秀程序员的习惯。为了尽量不影响你的用户的浏览,我们可以用把新的apache驱动在8080端口上或者用指令控制 mod_gzip起作用的目录,而不是一下子全用mod_gzip.
    用法如下:
    MOD_GZIP configuration

    没有问题后你就可以让你的用户很开心的发现’XX网站现在好快哦。’
    Mod_gzip真的很神奇,100K的HTML大 文档只要12K就可以传到用户端了。越先采用这个技术你的用户对你的网站的高速度印象就越深。不过有所得必有所失,由于解压是在客户端进行的,效果和用户 的浏览器有一定关系。

    2、IIS启用gzip

    现代的浏览器IE6和Firefox都支持客户端Gzip,也就是说,在服务器上的网页,传输之前,先使用Gzip压缩再传输给客户端,客户端接收之后由浏览器解压显示,这样虽然稍微占用了一些服务器和客户端的CPU,但是换来的是更高的带宽利用率。对于纯文本来讲,压缩率是相当可观的。如果每个用户节约50%的带宽,那么你租用来的那点带宽就可以服务多一倍的客户了。

    IIS6已经内建了Gzip压缩的支持,可惜,没有设置更好的管理界面。所以要打开这个选项,还要费些功夫。

    首先,如果你需要压缩静态文件(HTML),需要在硬盘上建一个目录,并给它“IUSR_机器名”这个用户的写权限。如果压缩动态文件(PHP,asp,aspx)就不需要了,因为它的页面是每次都动态生成的,压缩完就放弃。然后在IIS管理器中,“网站”上面右键-属性,不是下面的某个站点,而是整个网站。进入“服务”标签,选上启用动态内容压缩,静态内容压缩。

    然后选中网站下面那个服务器扩展,新建一个服务器扩展。名字无所谓,下面的添加文件的路径是:

    c:\windows\system32\inetsrv\gzip.dll,然后启用这个扩展。

    这时候静态内容是可以压缩的,但是对于动态内容,aspx文件却不在压缩范围内。因为默认的可压缩文件并没有这个扩展名。而管理界面中你又找不到可以增加扩展名的地方,这时候只能去修改它的配置文件了。

    在 c:\windows\system32\inetsrv\下面有个MetaBase.xml文件,可以用记事本打开,找到 IIsCompressionScheme,有三个相同名字的段,分别是deflate,gzip,Parameters,第三段不用管它,前两段有基本相同的参数,在这两段的参数HcScriptFileExtensions下面都加上一行aspx,如果你有其它的动态程序要压缩,也加在这里。 HcDynamicCompressionLevel改成9,(0-10,9是性价比最高的一个)。

    1.首先备份 IIS 的配置文件,

    复制 C:\Windows\system32\inetsrv\metabase.xml 到另外的备份文件夹中.

    C:\Windows\system32\inetsrv\metabase.xml 是 IIS 的核心配置文件,该文件的完整性一但被破坏,IIS 将无法正常运行,严重到需要重新安装系统.

    2. 在开始菜单中启动 Internet 信息服务(IIS)管理器,右键点击“网站”属性,打开“服务”选项卡,勾选“HTTP 压缩”的两个选项。“临时目录”和“临时目录最大容量”可根据需要自行设置。设置完成后点击确定。

    3. 右键点击“网站”下方的 “Web服务扩展”,添加一个新的Web服务扩展,扩展名填写为“HTTP Compression”或其他,都可以。“要求的文件”添加:c:\windows\system32\inetsrv\gzip.dll ,并勾选“设置扩展状态为允许”,完成后点击确定。

    4.下面的步骤有些复杂,如果没有确定的把握能理解,最好不要尝试,右键点击“Internet 信息服务的”“本地计算机”属性,勾选“允许直接编辑配置数据库”并确定。

    5. 在开始菜单中运行 notepad C:\Windows\system32\inetsrv\metabase.xml ,打开metabase.xml 文件,请在任何改动前再次确认该文件已经备份。

    6. 搜索并找到 metabase.xml 文件中的 <IIsCompressionScheme  片段中的 HcScriptFileExtensions,依照原有文件的格式添加你希 望进行 HTTP 压缩的文件扩展名,静态文件HcFileExtensions可添加 JS,CSS 等; 动态文件 HcScriptFileExtensions 可添加"ASPX"”ASMX”,如下:

    <IIsCompressionScheme Location =”/LM/W3SVC/Filters/Compression/deflate”
    HcCompressionDll=”%windir%\system32\inetsrv\gzip.dll”
    HcCreateFlags=”0″
    HcDoDynamicCompression=”TRUE”
    HcDoOnDemandCompression=”TRUE”
    HcDoStaticCompression=”FALSE”
    HcDynamicCompressionLevel=”9″
    HcFileExtensions=”htm
    html
    js
    css
    txt”
    HcOnDemandCompLevel=”10″
    HcPriority=”1″
    HcScriptFileExtensions=”asp
    aspx
    asmx
    dll
    exe”
    >
    </IIsCompressionScheme>
    <IIsCompressionScheme Location =”/LM/W3SVC/Filters/Compression/gzip”
    HcCompressionDll=”%windir%\system32\inetsrv\gzip.dll”
    HcCreateFlags=”1″
    HcDoDynamicCompression=”TRUE”
    HcDoOnDemandCompression=”TRUE”
    HcDoStaticCompression=”TRUE”
    HcDynamicCompressionLevel=”9″
    HcFileExtensions=”htm
    html
    js
    css
    txt”
    HcOnDemandCompLevel=”10″
    HcPriority=”1″
    HcScriptFileExtensions=”asp
    aspx
    asmx
    dll
    exe”
    >
    </IIsCompressionScheme>

    注意“Compression/deflate”和“Compression/gzip”两个片段都需要修改。动态压缩等级,HcDynamicCompressionLevel 建议设置为“9”

    7. 保存并关闭 metabase.xml 文件。

    8. 重新启动 IIS 服务,运行“IISReset”或重新启动 WWW 服务。

    9. 其他:

    如果你的服务器上某一个Web站点不希望启用 IIS 压缩,请运行以下脚本:

    cscript. C:\Inetpub\AdminScripts\adsutil.vbs set w3svc/site#/root/DoStaticCompression False
    cscript. C:\Inetpub\AdminScripts\adsutil.vbs set w3svc/site#/root/DoDynamicCompression False

    如果想增加启用 HTTP 压缩的文件类型,请运行以下脚本:

    “” 中为文件扩展名,别忘了把原有的扩展名加上。

    静态文件压缩:

    CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/GZIP/HcFileExtensions “htm” “html” “txt” “doc” “ppt” “xls”

    CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcFileExtensions “htm” “html” “txt” “doc” “ppt” “xls”

    动态文件压缩:

    CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions “asp” “aspx” “asmx” “dll” “exe” “php”

    CSCRIPT.EXE ADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcScriptFileExtensions “asp” “aspx” “asmx” “dll” “exe” “php”

    将site#替换为该 Web 站点的 ID。Web 站点的 ID 是什么到“日志记录属性”中看一下日志记录的“日志文件名”前面的路径就知道了。

    运行以上脚本前务必备份,运行后再次执行“IISReset”使生效.

    ######################################################################################

    apache2.0x 开启gzip压缩和http缓存的配置方法

    核心提示:上面的配置是我自己的服务器上正在使用的配置方法,在这里与大家分享一下,代码不会有错,最后不要忘记重启apache服务器。

    先谈gzip的配置方法,在apache2.0以上(包括apache2.0)的版中gzip压缩使用的是mod_deflate模块,下面是具体配置步骤:

    第1步

    LoadModule deflate_module modules/mod_deflate.so

    LoadModule headers_module modules/mod_headers.so

    打开httpd.conf后,先将上面两行配置前面的#号去掉,这样apache就会启用这两个模块,其中mod_deflate是压缩模块,就是对要传输到客户端的代码进行gzip压缩;mod_headers模块的作用是告诉浏览器页面使用了gzip压缩,如果不开启mod_headers那么浏览器就会对gzip压缩过的页面进行下载,而无法正常显示。

    第2步

    在httpd.conf中加入以下代码,可以加到任何空白地方,不了解apache的朋友,如果担心加错地方,就放到http.conf文件的最后一行。

     <IfModule mod_deflate.c>
        SetOutputFilter DEFLATE    #必须的,就像一个开关一样,告诉apache对传输到浏览器的内容进行压缩

        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary #设置不对后缀gif,jpg,jpeg,png的图片文件进行压缩
        SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary #同上,就是设置不对exe,tgz,gz。。。的文件进行压缩
        SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary

        AddOutputFilterByType DEFLATE text/* #设置对文件是文本的内容进行压缩,例如text/html  text/css  text/plain等
        AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript #这段代码你只需要了解application/javascript application/x-javascript这段就可以了,这段的意思是对javascript文件进行压缩
        AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp #这段是告诉apache对php类型的文件进行压缩

        BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.x 有一些问题,所以只压缩文件类型是text/html的
        BrowserMatch ^Mozilla/4.0[678] no-gzip # Netscape 4.06-4.08 有更多的问题,所以不开启压缩
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # IE浏览器会伪装成 Netscape ,但是事实上它没有问题

        Header append Vary User-Agent env=!dont-vary #确保代理不会发送错误的内容
    </IfModule>

    本配置到这里apache中的gzip压缩就配置完成,重启apache后,新的配置就会生效,当然这里我们还不会重启apache因为下一步要配置http缓存,也就是mod_expires模块。

    第1步

    LoadModule expires_module modules/mod_expires.so

    在httpd.conf中,找到上面的代码,然后将前面的#去掉

    第2步

    在httpd.conf中加入以下代码,可以加到任何空白地方,不了解apache的朋友,如果担心加错地方,就放到http.conf文件的最后一行。

     <IfModule mod_expires.c>
        ExpiresActive On #激活http缓存,也就是个开关,必须有的一段代码
        ExpiresDefault A604800 #默认缓存时间为604800秒,也就是7天,A表示以客户端时间为准
        ExpiresByType text/css A3600 #对css文件缓存3600秒,也就是1小时,A表示以客户端时间为准
        ExpiresByType text/html A3600 #对html文件缓存3600秒,也就是1小时,A表示以客户端时间为准
        ExpiresByType application/x-javascript A3600 #对javascript文件缓存3600秒,也就是1小时,A表示以客户端时间为准
        ExpiresByType image/gif "access plus 2 month" #对gif图片缓存2个月,以客户端时间为准
        ExpiresByType image/jpeg "access plus 2 month" #对jpeg和jpg图片缓存2个月,以客户端时间为准
        ExpiresByType image/png "access plus 2 month" #对png图片缓存2个月,以客户端时间为准
        ExpiresByType image/x-icon "access plus 2 month" #对浏览器小图标缓存2个月,以客户端时间为准
        ExpiresByType application/x-shockwave-flash A2592000 #对flash文件缓存2592000秒,也就是1个月,A表示以客户端时间为准

        #特别注意也可以使用 ExpiresByType text/css M3600  这里的M表示以文件最后修改时间为准缓存1小时,例如有些时候,我们的HTML是大批量定时更新的,这个时候就可以用到M

      Header unset Pragma #删除掉http头信息中的Pragma,不懂的可以google一下Pragma,他也是控制浏览器缓存的,不过是用于http1.0标准
      FileETag None
      Header unset ETag #这段代码和上面一段的作用是不使用http1.1标准中的ETag属性
      <FilesMatch "\.(js|css|ico|pdf|flv|jpg|jpeg|png|gif|mp3|mp4|swf)$"> #针对js|css|ico等后缀的文件进行单独设置
      #Header set Expires "Thu, 15 Apr 2013 20:00:00 GMT"
      Header unset Last-Modified #不使用http头信息中的Last-Modified属性,Last-Modified是指文件最后修改时间
      Header append Cache-Control "public" #设置为可被任何缓存区缓存
      </FilesMatch>
    </IfModule>

    上面的配置是我自己的服务器上正在使用的配置方法,在这里与大家分享一下,代码不会有错,最后不要忘记重启apache服务器哦!

    如果有apache高人有更好的配置,请指教一下!

    如果用的虚拟主机,而虚拟主机支持.htaccess,则可以将上面的代码放到.htaccess文件中

    ################################################################################

    Apache性能优化——开启gzip压缩

    服务器上跑了两个网站,一个是cacti,一个是自己的博客,均为PHP语言网站,虽然访问量不大,但是不管是cacti还是博客,里面含有大量图片,浏览起来速度并不是自己想象的那么理想,于是便对Apache简单做了下优化——启用gzip压缩功能。网站启用gzip压缩功能对于提升网站性能是非常明显的,首先是减少了服务器使用的带宽,其次是提高了用户浏览网站的速度。当然也有弊端,就是增加了硬件负载。最终的优化结果还是不错的,网站访问速度确实提升了不少http://blog.luwenju.cn。下面我们看一下Linux下如何开启Apache的gzip压缩功能的

    一、生成gzip压缩模块
    Apache的gzip功能是使用mod_deflate.so模块,因此,我们先生成mod_deflate.so模块。首先从网上下载一个Apache安装程序(尽量与当前使用的Apache版本相同)然后解压,我们解压到了tmp目录下
    #cd /tmp/httpd-2.2.11/modules/filters
    #/usr/local/apache/bin/apxs -i -c -a mod_deflate.c     //生成mod_deflate.so模块

    这时候在Apache安装目录的modules文件夹下应该就多了一个mod_deflate.so模块文件
    #ls /usr/local/apache/modules/
    httpd.exp  libphp5.so  mod_deflate.so


    二、在Apache中开启gzip压缩
    打开Apache配置文件,然后添加mod_deflate.so模块
    #vi /usr/local/apache/conf/httpd.conf
    LoadModule deflate_module     modules/mod_deflate.so



    三、设置gzip压缩
    打开Apache配置文件,然后添加如下三行内容
    #vi /usr/local/apache/conf/httpd.conf
    AddOutputFilterByType DEFLATE text/html text/php text/png text/jpg text/plain text/css text/xml text/javascript
    DeflateCompressionLevel 9
    SetOutputFilter DEFLATE

    上述参数解释:
    AddOutputFilterByTy         对什么样的内容启用GZIP压缩
    DeflateCompressionLevel     表压缩级别
    SetOutputFilter DEFLATE     启用deflate模块对本站点的所有输出进行GZIP压缩

    四、重启Apache

    ##########################################################################

    Why echo is slow in PHP and how to make it really fast

    12/03/09

    English (US) Why echo is slow in PHP and how to make it really fast

    Permalink 06:40:00 am, by Francois Planque Email , Categories: PHP, Web Dev, Linux stuff

    You may have noticed that PHP scripts that echo a lot of content appear to be running with poor performance…

    Well, the operative word here is “appear". It is a common misconception that “echo is the slowest PHP command"! :p

    The problem is actually just a bandwidth issue! When you try to pump a lot of content though the Internet, at some point you experience “load time"… and at some point PHP actually experiences “send time"!

    You may measure the execution time between the begining and the end of your script, and, on a slow connection, it may show you that it took 500 ms to execute. You may even narrow it down to a single echo statement that takes 480 ms to execute. But that time actually includes wait time where PHP cannot send any more data back to apache!

    There is a common trick that consists of starting output befering before echoing, like this:

    PHP:

      ob_start(); 
      echo $a_lot_of_content;

    This will allow PHP to move on and appear to terminate fast. But the truth is, all the content is now in PHP’s output buffer, and although your script is done, PHP is still working in the background to send all that data to your web server (apache for instance).

    ...

    To verify that the processing time doesn’t vary by employing this trick, you can log actual request processing times in apache.

    Now, if you want PHP to really terminate, you need to give Apache a larger buffer in order to absorb the content from PHP. I believe the default Apache buffer is 4096 or 8192 bytes (anyone know how to verify this?).

    If your PHP script tries to output 24 KB of data but apache can only buffer 8 KB, then yes PHP will be waiting. However, if you make the Apache cahce, say 32 KB, then PHP will send all its data to Apache and exit in a breeze!

    Here is how you can change Apache’s buffer size in apache2.conf :

    Code:

      <IfModule mpm_prefork_module>
         StartServers          5
         MinSpareServers       5
         MaxSpareServers      15
         MaxClients           40
         MaxRequestsPerChild  20
         SendBufferSize    32768
      </IfModule>

    The only important line in the SendBufferSize line. The other lines are provided for context so you can find the right place in apache2.conf to add the SendBufferSize line (in case it doesn’t exist already). I don’t have particular recommendations on the other lines at this point.

    After restarting apache, look at your logs and execution times again. You will see them drop from 500 ms to 30 ms in the example above. That is, of course, as long as you don’t send more data in PHP than the Apache buffer can absorb.

    Now, of course, this will *not* increase the end-to-end transfer time of the page from PHP to browser. However, it will allow PHP to exit faster, which may or may not free up resources on the server… probably depending on the configuration.

    I am pretty sure it does free up resources if you use CGI. But if you use mod_php, I don’t know… Anyone?

    Then, I guess, the next step would be to push the data out of Apache faster… but I’m not sure there is another buffer that could be augmented in between Apache and the actual Nagle Algorithm that manages the transfer speed.

  • 相关阅读:
    20172314 2017-2018-2 《程序设计与数据结构》第七周学习总结
    20172314 2017-2018-2 《程序设计与数据结构》第六周学习总结
    20172314 2017-2018-2 《程序设计与数据结构》第5周学习总结
    20172314 2017-2018-2 《程序设计与数据结构》实验报告一
    20172314 2017-2018-2 《程序设计与数据结构》 第三周学习总结
    20172314 2017-2018-2 《程序设计与数据结构》第一周学习总结
    预备作业03
    学号 2017-2018-20172309 《程序设计与数据结构》第3周学习总结
    # 学号 2017-2018-20172309 《程序设计与数据结构》实验1报告
    第二 周作业总结
  • 原文地址:https://www.cnblogs.com/kingangWang/p/2287602.html
Copyright © 2011-2022 走看看