Apache 開啟 gzip 壓縮方法:
這裡我使用的是 Apache2.4.17
開啟 apache 安裝目錄,找到 conf 目錄,用記事本開啟 httpd.conf 檔案。
ctrl+f 查詢
去掉 #LoadModule headers_module modules/mod_headers.so 前面的註釋 #
去掉 #LoadModule deflate_module modules/mod_deflate.so 前面的註釋 #
去掉 #LoadModule filter_module modules/mod_filter.so 前面的註釋 #
檔案末尾加上
SetOutputFilter DEFLATE
DeflateCompressionLevel 6
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript //需要壓縮的格式
AddOutputFilter DEFLATE css js txt xml rss html htm //需要壓縮的格式
Header append Vary User-Agent env=!dont-vary
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png|bmp|tif)$ no-gzip dont-vary //不需要壓縮的格式
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|7z|bz2|sit|rar|bin|iso)$ no-gzip dont-vary //不需要壓縮的格式
SetEnvIfNoCase Request_URI .(?:pdf|doc|docx|xls|xlsx|ppt|pptx)$ no-gzip dont-vary //不需要壓縮的格式
然後看客戶端的請求裡是否有 :
Accept-Encoding: gzip, deflate //代表客戶端支援 gzip
服務端的響應裡是否有 :
Content-Encoding: gzip //代表服務端已開啟 gzip
有些站群伺服器對內容進行 gzip 編碼只針對某些檔案, 所以 有沒有返回 這個, 並不能代表, 是否支援 gzip 的依據。
Apache 配置 Expire/Cache-Control 頭
開啟 apache 安裝目錄,找到 conf 目錄,用記事本開啟 httpd.conf 檔案。
ctrl+f 查詢 LoadModule expires_module modules/mod_expires.so
去掉前面 #號!
在文字最後面新增:
#開啟 WordPress 加速快取
ExpiresActive on
#css 檔案 WordPress 加速快取 7200000/3600/24=83 天
ExpiresByType text/css A7200000
#js 檔案 WordPress 加速快取 83 天
ExpiresByType application/x-javascript A7200000
ExpiresByType application/javascript A7200000
#html 檔案 WordPress 加速快取 83 天
ExpiresByType text/html A7200000
#圖片檔案 WordPress 加速快取 83 天
ExpiresByType image/jpeg A7200000
ExpiresByType image/gif A7200000
ExpiresByType image/png A7200000
ExpiresByType image/x-icon A7200000
上面開啟的是 expire
下面是 cache-control
在文字後面繼續新增
Header set Cache-Control “max-age=604800, public”
Header set Cache-Control “max-age=604800, public”
這裡時間設定不一樣是為了檢驗是否成功配置的,因為,沒設 cache-control 的時候,它會自動根據 expire 的時間設定自己。
最後重啟 apache 站群伺服器,ok!
這裡說一個自己爬的坑吧!(那就是如果不出現 200 form cache, 而是出現了 304,那是因為,你重新整理了瀏覽器。。。想要出現 200 form cache,需要在瀏覽器位址列裡按回車鍵。)
也就是說:重新整理瀏覽器 觸發 304, 位址列回車觸發 200 form cache 。
查了好久才發現了,之前一直想不通,既然設定了 cache-control 和 expire 為啥還是 304,這就是原因,就是這麼簡單。