mod_deflate, mod_gzip (apache deflate)

最後更新: 2017-06-28

目錄

 

介紹

mod_deflate 是一個 Apache 的 input/output filter 來 (舊版(2.2): mod_gzip)

它多數運用在 Output 時, 用作將某類型資料壓縮(i.e. *.css, *.js)

 


mod_deflate

 

啟用 mod_deflate

# Debian

a2enmod deflate

/etc/init.d/apache2 restart

# Centos 6

grep deflate /etc/httpd/conf/httpd.conf

LoadModule deflate_module modules/mod_deflate.so

設定檔

# Debian

/etc/apache2/mods-available/deflate.conf

對某類型檔 gzip:

[方法1] By Filename Extension

# FilesMatch

<IfModule mod_deflate.c>
    <filesMatch "\.(js|css)$">
        SetOutputFilter DEFLATE
    </filesMatch>
</IfModule>

[方法2] By File Type(MIME)

1) Add FileType

參考: https://datahunter.org/apache_mime

# Add Filter To Type

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE application/javascript
</IfModule>

可以寫成單一行

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript

[方法3] 對某 Directory (container) 進行設定

<Directory "/var/www/datahunter/manual">
    # 除了zip檔及圖片外, 所有東西都 DEFLATE filter
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI \.zip$                no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$  no-gzip dont-vary
</Directory>

Testing

Response Headers:

...
Content-Encoding:gzip
...

 * gzip 後 size 大於或等於 32 byte 才會壓縮 (Content-Length: 32)

壓縮比

jquery-3.6.0.min.js

Content-Length: 89501    # 無壓
Content-Length: 30902    # 壓了

Settings

# 1 (less compression) and 9 (more compression). cli gzip default: -6

DeflateCompressionLevel 9

# How much memory should be used by zlib for compression

DeflateMemLevel 9

# zlib compression window size 1~15.

# higher the window size, the higher can the compression ratio be expected

DeflateWindowSize 15

 


Speedup by Pre-Gzip

 

 * 需要 mod_headers 及 mod_rewrite

RewriteEngine On

#### 如何對待 .css.gz 及 .js.gz
# Serve correct content types, and prevent mod_deflate double gzip
RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1,END]
RewriteRule "\.js\.gz$"  "-" [T=application/javascript,E=no-gzip:1,END]

<FilesMatch "(\.js\.gz|\.css\.gz)$">
  # Serve correct encoding type
  Header append Content-Encoding gzip

  # Force proxies to cache gzipped & non-gzipped css/js files separately
  Header append Vary Accept-Encoding

  # 知到 Pre-compress 了
  Header set Pre-compress 1
</FilesMatch>

#### Serve gzip CSS & JS files if they exist and the client accepts gzip
RewriteCond "%{REQUEST_FILENAME}\.gz" -s
RewriteCond "%{HTTP:Accept-encoding}" "gzip"
RewriteRule "\.(?:css|js)$" "%{REQUEST_URI}.gz" [L]

測試

curl -I -H"Accept-Encoding: gzip" https://datahunter.org/css/92f8dff.css

ETag: "116bee9-5f377903302ca-gzip"    # mod_deflate
ETag: "116bee9-5f377903302ca"         # rewrite 生效

說明

使用 "-s" 而不是 "-f", -s => regular file with size greater than zero

RewriteCond "%{REQUEST_FILENAME}\.gz" -s

使用 non-capturing group 及 L flag

RewriteRule "\.(?:css|js)$" "%{REQUEST_URI}.gz" [L]

Browser 的 request 有

Accept-Encoding: gzip, deflate, br

 


mod_gzip

 

Enable compression on Apache webservers (可用於 .htaccess)

<ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
</ifModule>

mod_gzip_dechunk

instructs the module to handle bursty (chunked) output from Perl or PHP scripts

 


 

Creative Commons license icon Creative Commons license icon