What are the advantages and disadvantages of enabling gzip in apache?

Source: Internet
Author: User
Tags bz2 rar zip

I think gzip compression is usually enabled for all websites, especially for large websites. Enabling gzip greatly improves transmission efficiency. Generally, the compression ratio can reach 75%, that is, KB, the compression size is only 25 kB.

 

Since gzip compression is so good, of course there is no doubt that it is enabled? Yes, we usually turn it on, especially for websites with high traffic, the effect after opening is too obvious. In apache, you only need to enable mod_headers.so and mod_deflate.so and then configure them slightly. You can also customize the compression ratio. The value ranges from 1 (minimum) to 9 (maximum). However, it is not recommended that you set the compression ratio to be too high, which will increase the cpu overhead.

 

Therefore, the advantage of enabling gzip is to greatly reduce the transmission size, increase the webpage response speed, and save bandwidth. As shown in the following figure, the compression effect is very obvious.

 

 

Behind the benefits, there must be few drawbacks. That is, after gzip is enabled, a lot of cpu overhead will be added, which puts a lot of pressure on the server. At the same time, the client decompression also requires overhead (but the client is fine ), this is why we do not recommend setting the compression rate too high. However, the current hardware is generally better, and it is very cost-effective to sacrifice some hardware to improve performance and user experience! Isn't it true that everyone is good? Only when the webpage is opened quickly is it really good.

 

The mod_deflate module is used for gzip compression in apache2.0 and later versions (including apache2.0). The configuration steps are as follows:
Step 2

LoadModule deflate_module modules/mod_deflate.soLoadModule headers_module modules/mod_headers.so

Open httpd. after conf, remove the # sign in front of the above two configurations, so that apache will enable these two modules, where mod_deflate is the compression module, it is to perform gzip compression on the code to be transmitted to the client. The mod_headers module is used to tell the browser that the page uses gzip compression, if mod_headers is not enabled, the browser downloads the compressed gzip page and cannot display it normally.
Step 2
In httpd. add the following code to conf to add it to any blank space. If you are not familiar with apache, put it in http if you are worried about adding an error. the last line of the conf file, which can be written by a virtual server. put the htaccess file in the project.

1. mod_gzip

<IfModule mod_deflate.c> SetOutputFilter DEFLATE # is required, just like a switch, to tell apache to compress the content transmitted to the browser. SetEnvIfNoCase Request_URI .(? : Gif | jpe? G | png) $ no-gzip dont-vary # do not compress the gif, jpg, jpeg, and png image files. SetEnvIfNoCase Request_URI .(? : Exe | t? Gz | zip | bz2 | sit | rar) $ no-gzip dont-vary # Same as above, that is, the settings are incorrect for exe, tgz, gz... SetEnvIfNoCase Request_URI .(? : Pdf | mov | avi | mp3 | mp4 | rm) $ no-gzip dont-vary AddOutputFilterByType DEFLATE text/* # Set to compress the text content of the file, for example, text/html text/css text/plain and other AddOutputFilterByType DEFLATE application/ms * application/vnd * application/postscript application/javascript application/x-javascript # you only need to understand the application /javascript application/x-javascript, this section indicates compressing javascript files. AddOutputFilterByType DEFLATE application/x-htt Pd-php application/x-httpd-fastphp # this section tells apache to compress a php-type file. BrowserMatch ^ Mozilla/4 gzip-only-text/html # Netscape 4.x has some problems., therefore, only the compression file type is text/html's BrowserMatch ^ Mozilla/4.0 [678] no-gzip # Netscape 4.06-4.08. There are more problems, so do not enable compression of BrowserMatch \ bMSIE! No-gzip! Gzip-only-text/html # IE will pretend to be Netscape, but in fact it is okay </IfModule>


2. deflate_Module mode

(1) strictly match the file type

# Mod_deflate: <ifmodule mod_deflate.c> DeflateCompressionLevel 6 # compression rate. 6 is the recommended value. includeflate text/plain includeflate text/html includeflate text/php AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css includeflate text/javascript includeflate application/xhtml + xml includefl ATE application/xml parse DEFLATE application/rss + xml AddOutputFilterByType DEFLATE application/atom_xml AddOutputFilterByType DEFLATE application/x-javascript parse DEFLATE application/x-httpd-php parse DEFLATE image/svg + xml parse 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

# Mod_deflate: <ifmodule mod_deflate.c> DeflateCompressionLevel 6 SetOutputFilter DEFLATE # compress all files # Don't compress images and other # filter files without compression # 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>

In this configuration, gzip compression in apache is configured. After apache is restarted, the new configuration will take effect. Finally, I used a tool to check whether the score was improved a lot ~ Haha. However, enabling the gzip function requires additional CPU resource overhead.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.