Apache 開啟gzip壓縮

大飛_dafei發表於2019-04-02

01) 開啟模組

LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
LoadModule filter_module modules/mod_filter.so

02) 配置

    <IfModule mod_deflate.c>
         #告訴瀏覽器內容進行壓縮
         # https://httpd.apache.org/docs/2.4/mod/mod_deflate.html#deflatecompressionlevel
         SetOutputFilter DEFLATE

         #壓縮級別1--9,預設為6
         # https://httpd.apache.org/docs/2.4/mod/mod_deflate.html#deflatecompressionlevel
         DeflateCompressionLevel 6

         #不對那些進行壓縮
         # https://httpd.apache.org/docs/2.4/mod/mod_setenvif.html#setenvifnocase
         SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
         SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
         SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary

         #對那些壓縮
         # https://httpd.apache.org/docs/2.4/mod/mod_filter.html#addoutputfilterbytype
         AddOutputFilterByType DEFLATE text/*
         AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript
         AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp

         #解決一下相容問題
         # https://httpd.apache.org/docs/2.4/mod/mod_setenvif.html#browsermatch
         BrowserMatch ^Mozilla/4 gzip-only-text/html
         BrowserMatch ^Mozilla/4.0[678] no-gzip
         BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    </IfModule>

 

相關文章