網頁gzip壓縮是個什麼概念呢?若是你還沒接觸到,講文字全是廢話,先來看兩張圖看着實在;javascript
網頁壓縮前:php
網頁壓縮後:css
從圖中就可看出網頁壓縮後這加載時間是成倍的縮減,下面就是就是要講解如何開啓gzip壓縮;html
實例環境:java
服務器:centos6.5apache
apche:2.2.15vim
gzip壓縮須要 deflate_module和headers_module的支持;系統默認就是開啓的;須要作的是在httpd.conf配置項添加規則;centos
[root@iZ28qa8jt4uZ conf]# vim /etc/httpd/conf/httpd.conf #修改配置項 #在最後邊添加以下內容 <Location /> AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/atom_xml AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/x-httpd-php AddOutputFilterByType DEFLATE text/html </Location> [root@iZ28qa8jt4uZ conf]# service httpd restart #重啓apache
ok就這麼簡單便可;而後經過火狐控制檯的網絡查看是否有Content-Encoding:gzip 這條
bash
或者經過站長工具來查看:http://tool.chinaz.com/Gzips/
服務器
什麼?你說你用的是虛擬空間;不能修改httpd.conf ?好吧,使用.htaccess總能夠吧,好人作到底;在項目根目錄下建一個.htaccess文件
裏面添加以下內容:
<IfModule mod_deflate.c> # Insert filters AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/x-httpd-php AddOutputFilterByType DEFLATE application/x-httpd-fastphp AddOutputFilterByType DEFLATE image/svg+xml # Drop problematic browsers BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch bMSI[E] !no-gzip !gzip-only-text/html # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary </IfModule>
若是是本身的服務器還想使用.htaccess;那須要在httpd.conf 裏面開啓.htaccess;
修改httpd.conf的302行左右 AllowOverride None 改成 AllowOverride All:
<Directory /> Options FollowSymLinks AllowOverride All </Directory>
還有大約340左右的AllowOverride None 改成 AllowOverride All:
AllowOverride All:
可是若是是本身的服務器,儘可能修改http.conf,而不是使用.htaccess。由於開啓.htaccess後apache每次都要檢測.htaccess文件會稍微下降httpd服務器的性能;