前端性能優化-gzip

爲何要開啓GZIPhtml

咱們須要下載一個100KB的Javascript文件,正常的下載量就是100KB,若是咱們把文件在服務端壓縮一下,壓縮成30kb,下載到客戶端再進行解壓,這樣就減小了大量的HTTP的傳輸時間,這就是GZIP的做用。jquery

如何開啓服務器的GZIPwindows

咱們以windows版的Apache2.4爲例,打開httpd.conf文件,由於開啓GZIP須要mod_deflate.so,因此首先把解注mod_deflate.so,而後增長下面的配置項:服務器

#開啓GZIPapp

<IfModule mod_deflate.c>
   SetOutputFilter DEFLATE
   # example of how to compress ONLY html, plain text and xml
   # AddOutputFilterByType DEFLATE text/plain text/html text/xml
   # Don't compress binaries
   SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|iso|tar|bz2|sit|rar) no-gzip dont-vary
   # Don't compress images
   SetEnvIfNoCase Request_URI .(?:gif|jpe?g|jpg|ico|png)  no-gzip dont-vary
   # Don't compress PDFs
   SetEnvIfNoCase Request_URI .pdf no-gzip dont-vary
   # Don't compress flash files (only relevant if you host your own videos)
   SetEnvIfNoCase Request_URI .flv no-gzip dont-vary
   # Netscape 4.X has some problems
   BrowserMatch ^Mozilla/4 gzip-only-text/html
   # Netscape 4.06-4.08 have some more problems
   BrowserMatch ^Mozilla/4.0[678] no-gzip
   # MSIE masquerades as Netscape, but it is fine
   BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
   # Make sure proxies don't deliver the wrong content
   Header append Vary User-Agent env=!dont-vary
   # Setup custom deflate log
   DeflateFilterNote Input instr
   DeflateFilterNote Output outstr
   DeflateFilterNote Ratio ratio
   LogFormat '"%r" %{outstr}n/%{instr}n %{ratio}n%%' DEFLATE
   CustomLog logs/deflate_log DEFLATE
</IfModule>

 

重啓Apache,配置生效!ide

對比測試測試

咱們以jquery-1.7.1.js爲例,在不開啓GZIP和開啓GZIP下的文件大小進行對比spa

1.開啓GZIP,文件傳輸大小爲32.7KBcode

image

2.未開啓GZIP,文件的傳輸大小爲92.0KB,即文件的實際大小orm

image

相關文章
相關標籤/搜索