当前位置:网站首页>Apache enables gzip compressed web page transmission method

Apache enables gzip compressed web page transmission method

2022-06-24 06:14:00 User 1685462

First of all, let's understand Apache Gzip Related information .

One 、gzip Introduce

Gzip Is a popular file compression algorithm , Now it's widely used , Especially in Linux platform . When an application Gzip When compressed to a plain text file , The effect is very obvious , It can be reduced 70% File size above . It depends on what's in the file . utilize Apache Medium Gzip modular , We can use Gzip Compression algorithm to Apache The web content published by the server is compressed and then transmitted to the client browser . This compression actually reduces the number of bytes transmitted on the network , The most obvious advantage is that it can speed up the loading of web pages .

The benefits of faster web loading are self-evident , Besides saving traffic , Improve the user's browsing experience , Another potential benefit is Gzip It has a better relationship with search engine grabbing tools .

Two 、Web The server processes

HTTP The compression process is as follows :

Web The server received HTTP After the request , Check whether the browser supports HTTP Compress (Accept-Encoding Information );

If the browser supports HTTP Compress ,Web The server checks the suffix of the request file ;

If the request file is HTML、CSS Etc. static files ,Web The server checks whether the latest compressed file of the requested file already exists in the compressed buffer directory ;

If the compressed file of the requested file does not exist ,Web The server returns the uncompressed request file to the browser , And store the compressed file of the request file in the compressed buffer Directory ;

If the latest compressed file of the requested file already exists , Then directly return the compressed file of the request file ;

If the request file is a dynamic file ,Web The server dynamically compresses the content and returns it to the browser , Compressed content is not stored in the compressed cache directory .

3、 ... and 、 Turn on Gzip

Apache utilize Gzip There are two modules for compression algorithm :mod_gzip and mod_deflate.

Now the browser itself automatically Gzip Compression , Support Accept-Encoding: gzip,deflate , Here I am firefox Test under Browser .

By looking at HTTP head , We can quickly determine whether the client browser we use supports acceptance gzip Compress .

If sent HTTP The following message appears in the header , It means that your browser supports accepting the corresponding gzip Compress :

Accept-Encoding: gzip Support mod_gzip

Accept-Encoding: deflate Support mod_deflate

Accept-Encoding: gzip,deflate

Support at the same time mod_gzip and mod_deflate Apache Built in mod_deflate Module to enable gzip function , But if installed apache There is no compilation of related modules , You need to install it manually , To enable it :

First come to your apache Source directory , Find the mod_deflate.c file , Usual location :apachehttpd Source directory /modules/filters/mod_deflate.c, Enter the directory found above and run the following command :

/usr/local/apache2/bin/apxs -i -c -a mod_deflate.c

notes :apxs Please refer to your own machine for the catalog , Usually in apache Installation directory bin Under the table of contents .

installation is complete , To apache Of modules Catalog to see if there is mod_deflates.so,httpd.conf Open in deflate_Module and headers_Module modular :

LoadModule deflate_module modules/mod_deflate.so

load mod_deflate.so modular , The default installation will automatically write httpd.conf.

If the server is enabled for Gzip Component support , Then we can be in http.conf Custom file compression , Here is a simple example of the configuration : 1、mod_gzip The way

The code is as follows :

# 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>

2、deflate_Module The way

(1) Strictly match file types

The code is as follows :

# 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/php
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
AddOutputFilterByType DEFLATE image/gif image/png image/jpe image/swf image/jpeg image/bmp
# Don't compress images and other # Exclude files that do not need to be compressed 
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 .(?: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
</ifmodule>

(2) Filter file types

The code is as follows :

# mod_deflate:
<ifmodule mod_deflate.c>
DeflateCompressionLevel 6
SetOutputFilter DEFLATE # Compress all files 
#Don't compress images and other # Filtering does not require compressed files 
#SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:html|htm)$ 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
</IfModule>

file MIME Types can be added according to your own situation , You can also view it through a browser connect-type:

Four 、mod_gzip and mod_deflate What's the main difference between ?( From the Internet )

The first difference is that they are installed Apache Web Differences in server versions .Apache 1.x Series does not have built-in web compression technology , That's why we use additional third parties mod_gzip Module to perform compression . and Apache 2.x When the government is developing , Take web compression into account , built-in mod_deflate This module , To replace mod_gzip. Although both are used Gzip Compression algorithm , They work on a similar principle . The second difference is compression quality .mod_deflate Slightly faster compression and mod_gzip The compression ratio is slightly higher . By default ,mod_gzip than mod_deflate More 4%~6% Amount of compression . that , Why use mod_deflate? The third difference is the use of server resources . Generally speaking mod_gzip To the server CPU The occupation of is higher .mod_deflate It is a compression module specially used to ensure the performance of the server ,mod_deflate Requires less resources to compress files . This means that in high traffic servers , Use mod_deflate It might be better than mod_gzip Faster loading .  

5、 ... and 、 effect

Has not started Gzip Compress :

start-up Gzip Compress :

Size by 10.7K To 1.5K, If the file is large , Then the effect will be more obvious .

原网站

版权声明
本文为[User 1685462]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/07/20210723184705069X.html