Home  >  Article  >  Operation and Maintenance  >  How to enable GZIP compression in Apache

How to enable GZIP compression in Apache

小云云
小云云Original
2018-03-05 09:50:241996browse

Gzip is a popular file compression algorithm that is now widely used, especially on the Linux platform. Gzip is supported by default in modern browsers. When a plain text file is compressed with Gzip, the file size can be reduced by more than 70%. After compression, the number of bytes transmitted over the network can be greatly reduced. The advantage of using Gzip is that it can speed up web pages. Loading speed also makes your website more search engine friendly. Below, PHP programmer Lei Xuesong explains in detail how Apache uses the mod_deflate module to implement Gzip compression of web content.

Apache server HTTP request Gzip processing process

1. After the Apache server receives the HTTP request from the browser, it checks whether the browser supports HTTP compression (Accept-Encoding information).

2. If the browser supports HTTP compression, the Apache server checks the suffix of the requested file.

a. If the requested file is a static file such as HTML, CSS, JS, Images, etc., the Apache server will check whether the latest compressed file of the requested file already exists in the compression buffer directory. If it does not exist, return the compressed requested file. , and store the compressed file in the compression buffer directory. If it already exists, directly return the compressed file of the requested file.
b. If the requested file is a dynamic file, the web server dynamically compresses the content and returns it to the browser. The compressed content is not stored in the compression cache directory.

Apache supports GZIP compression configuration

1. Edit the http.conf file and enable the mod_deflate module

Remove #LoadModule headers_module modules/mod_headers.so The previous comment
#Remove the #LoadModule deflate_module modules/mod_deflate.so The previous comment
#Remove the #LoadModule filter_module modules/mod_filter.so The previous comment

#2. Configure Gzip compression related parameters


<IfModule mod_deflate.c>
#压缩率,6是建议值。
DeflateCompressionLevel 6
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 image/svg+xml
AddOutputFilterByType DEFLATE image/gif image/png image/jpe image/swf image/jpeg image/bmp
#排除不需要压缩的文件
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI .(?:html|htm)$ no-gzip dont-varySetEnvIfNoCase
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary
</IfModule>

3. Restart the Apache server and use a browser request to verify whether Gzip is effective.

Related recommendations:

Use PHP to determine whether the server supports Gzip compression function_PHP tutorial

PHP zlib extension page GZIP compression function detailed explanation

apache gzip compression settings


The above is the detailed content of How to enable GZIP compression in Apache. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn