apache下配置gzip和靜態檔案快取

來源:互聯網
上載者:User

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在很多情況下需要客戶短開啟緩衝,檔案開啟gzip壓縮來加快頁面載入速度。</span>

一.首先看下開啟頁面緩衝(也就是經常說的頁面到期時間)

1.apache中需要增加

LoadModule expires_module modules/mod_expires.so

如果是前面加了警號需要把警號去掉

2.增加如下代碼

<IfModule expires_module>
#開啟緩衝
ExpiresActive on
#檔案快取864000/3600/24=10天
ExpiresByType text/css A864000
ExpiresByType application/x-javascript A864000
ExpiresByType application/javascript A864000
ExpiresByType text/html A864000
ExpiresByType image/jpeg A864000
ExpiresByType image/gif A864000
ExpiresByType image/png A864000
ExpiresByType image/x-icon A864000
</IfModule>

ExpiresByType 是檔案的MIME類型 可以直接指定到期的類型

重啟apache 伺服器 service httpd restart

重新重新整理幾下網頁

在chrome 下可以看到

  1. Cache-Control:max-age=864000Connection:closeDate:Wed, 28 May 2014 06:57:55 GMTETag:"d9233e-452-4fa5f3d0de300"Expires:Sat, 07 Jun 2014 06:57:55 GMTServer:Apache/2.2.15 (CentOS)

  2. 可以看到有效期間Cache-control;到期時間是10天

    二. 開啟gzip壓縮 apache有兩種壓縮方式 mod_deflate 和 mod_gzip 以 mod_deflate為例子 先看看是否開啟了對應的模組 LoadModule deflate_module modules/mod_deflate.so如果已經開啟了直接在 httpd.conf 或者在 .htaccess 檔案中 增加
    <ifmodule mod_deflate.c>DeflateCompressionLevel 6 AddOutputFilterByType DEFLATE text/plainAddOutputFilterByType DEFLATE text/htmlAddOutputFilterByType DEFLATE text/xmlAddOutputFilterByType DEFLATE text/cssAddOutputFilterByType DEFLATE text/javascriptAddOutputFilterByType DEFLATE application/xhtml+xmlAddOutputFilterByType DEFLATE application/xmlAddOutputFilterByType DEFLATE application/rss+xmlAddOutputFilterByType DEFLATE application/atom_xmlAddOutputFilterByType DEFLATE application/x-javascriptAddOutputFilterByType DEFLATE application/x-httpd-phpAddOutputFilterByType DEFLATE image/svg+xml</ifmodule>

    1. 重啟apache就可以了 重新整理chrome可以看到 size 的大小

    2. 壓縮比例還是不錯的!關於 mod_deflate 和 mod_gzip 可以參照 http://www.xmydlinux.org/201108/604.html MIME 類型可以參照 http://www.iana.org/assignments/media-types/media-types.xhtml



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.