Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> 電腦軟件教程 >> 服務器技術 >> 關於服務器 >> Apache開啟Gzip和Expires提升網頁加載速度

Apache開啟Gzip和Expires提升網頁加載速度

日期:2017/2/8 10:28:28      編輯:關於服務器

在Apache的配置文件中找到下面兩行,將注釋#去掉,重啟。

LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so

在你網站的目錄下新建.htaccess,添加如下內容:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A604800
ExpiresByType text/css A604800
</IfModule>

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css image/gif image/jpeg image/png application/x-javascript
</IfModule>

解釋一下。ExpiresByType 是通過MIME類型來設置具體文件的緩存時間,A表示訪問,A後面的數字表示訪問後的緩存時間。AddOutputFilterByType表示根據後面的MIME類型來壓縮文件,這裡對css,html,gif,jpeg,png,JavaScript等進行gzip壓縮。更多選項請參考apache手冊哦。

關閉ETag。Etag的問題在於,它是根據可以辨別網站所在的服務器的具有唯一性的屬性來生成的。當浏覽器從一台服務器上獲得頁面內容後到另外一台服務器上進行驗證時ETag就會不匹配,這種情況對於使用服務器組和處理請求的網站來說是非常常見的。在配置文件中寫入下面一行即可:

FileETag none

如果是多服務器負載均衡,可以設置為FileETag MTime Size,apache默認設置為FileETag INode MTime Size,去掉INode。

經過上面的設置,即可開啟壓縮和緩寸.

Copyright © Windows教程網 All Rights Reserved