Enable the deflate_Module and headers_Module modules in httpd. conf.
Remove the preceding two sentences:
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
Add the following code at the bottom of the httpd. conf file to configure the file to be compressed:
<IfModule deflate_module>
SetOutputFilter DEFLATE
# Don't compress images and other
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 | doc) $ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
Check whether Gzip is enabled
Apache uses the Gzip compression algorithm to compress modules: mod_gzip and mod_deflate.
Now the browser also supports the automatic Gzip compression function, which supports Accept-Encoding: gzip and deflate. Here I will test it in the firefox browser.
By viewing the HTTP header, we can quickly determine whether the client browser can accept gzip compression.
If the server enables support for Gzip components, you can customize them in http. conf or. htaccess. Below is a simple example of. htaccess configuration:
The code is as follows: |
Copy code |
# Mod_gzip: <IfModule mod_gzip.c> Mod_gzip_on Yes Mod_gzip_dechunk Yes Mod_gzip_item_include file \. (html? | Txt | css | js | php | pl) $ Mod_gzip_item_include handler ^ cgi-script $ Mod_gzip_item_include mime ^ text /.* Mod_gzip_item_include mime ^ application/x-javascript .* Mod_gzip_item_exclude rspheader ^ Content-Encoding:. * gzip .* <IfModule> # Mod_deflate: <Ifmodule mod_deflate.c> DeflateCompressionLevel 6 # compression ratio, 6 is the recommended value. AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE application/xhtml + xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/rss + xml AddOutputFilterByType DEFLATE application/atom_xml AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/x-httpd-php AddOutputFilterByType DEFLATE image/svg + xml </Ifmodule> |
The file MIME type can be added according to your own situation. As for PDF files, images, music files, and other files, these files are already highly compressed, and repeated compression does not play a major role, however, the performance may be reduced by increasing the CPU processing time and rendering of the browser. Therefore, it is unnecessary to use Gzip compression. After the preceding settings, check the returned HTTP header. If the following information is displayed, the returned data has been compressed. That is, the Gzip compression configured by the website program takes effect.
Content-Encoding: gzip
Note: No matter whether mod_gzip or mod_deflate is used, the information returned here is the same. Because they are all implemented gzip compression methods.