当前位置:网站首页>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 .
边栏推荐
- 10 year old drivers who have been engaged in software testing tell you what type of software is suitable for automation
- What is the reason why the list of channels on the left side of easycvr video Plaza displays garbled codes?
- Multi objective Optimization Practice Based on esmm model -- shopping mall
- Malicious software packages are found in pypi code base. Tencent security threat intelligence has been included. Experts remind coders to be careful of supply chain attacks
- Material production tool manual
- New core and new speed - next generation standard O & M engine
- Analysis of official template of wechat personnel recruitment management system (III)
- Spirit information development log (4)
- Feign request return value inverse sequence localdatetime exception record
- Koa middleware implementation
猜你喜欢

Solution to the 39th weekly game of acwing

What is the difference between a white box test and a black box test

One line of keyboard

A cigarette of time to talk with you about how novices transform from functional testing to advanced automated testing

ServiceStack. Source code analysis of redis (connection and connection pool)
![[fault announcement] one stored procedure brings down the entire database](/img/7c/e5adda73a077fe4b8f04b59d1e0e1e.jpg)
[fault announcement] one stored procedure brings down the entire database

Technology is a double-edged sword, which needs to be well kept
随机推荐
Wireshark grabs the RTSP stream of easynvr without displaying RTSP. Solution
Introduction of frequency standard comparison measurement system
Project deployment for learning 3D visualization from scratch
CLB unable to access / access timeout troubleshooting
Tencent security release data security compliance capability map
Tensorflow daily essay (I)
Web automated testing (2): choose selenium advantage? Comparison with phantomjs/qtp/monkey
Typora software installation
5 minutes, online from 0 to 1!
Risc-v assembly language programming (2) assembly program ASM_ run_ led
Continuously evolving cloud native application delivery
PMP | 8 abilities that excellent project managers focus on training
Analysis on the influence of "network security policy issued successively" on Enterprises
[industry outlook] future development forecast of UHD video application
Brief introduction to the working principle of high frequency signal generator
The errorcontrol registry of the third-party service is 3, which may cause the system to cycle restart. For example, ldpkit introduced by WPS
How to recover data by splicing database fragments
Easycvr is cascaded to easygbs through gb28181 protocol. Notes on video channel failure
Web automation test (3): Selenium basic course of web function automation test
Analysis of DDoS attack methods