How to enable gzip compression in apache

Source: Internet
Author: User
Tags http request cpu usage

Start gzip of apache2

The following lines are found in http. conf. If not, add

# LoadModule deflate_module modules/mod_deflate.so remove. Indicates that GZIP is enabled.

More customization

The compression function is enabled. It can be used very well. For more detailed customization, see the following:

You can add this section after http. conf for customization: (if not necessary, use the default one)

<Ifmodule mod_deflate.c>
DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
AddOutputFilter DEFLATE js css
</Ifmodule>

DeflateCompressionLevel 9 refers to the compression level, from 1 to 9, 9 is the highest level. It is understood that this can reduce the transmission volume by up to 8 characters (depending on the file content), and reduce the transmission volume by at least half.

By default, DeflateCompressionLevel can use the value 6 to maintain a balance between the consumed processor performance and the web page compression quality.


The Web server processes HTTP compression as follows:
1. After receiving the HTTP request from the browser, the Web server checks whether the browser supports HTTP compression (Accept-Encoding information );

2. If the browser supports HTTP compression, the Web server checks the suffix of the request file;

3. If the requested file is a static file such as HTML and CSS, the Web server will check whether the latest compressed file of the requested file already exists in the compressed buffer directory;

4. If the compressed file of the request file does not exist, the Web server returns the uncompressed request file to the browser and stores the compressed file of the request file in the compressed buffer directory;

5. If the latest compressed file of the request file already exists, the compressed file of the request file will be directly returned;

6. If the request 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 compressed cache directory.

 


Step 2
LoadModule deflate_module modules/mod_deflate.so
LoadModule 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
Add the following code to httpd. conf to add it to any blank space. If you are not familiar with apache, put it in the last line of the http. conf file if you are worried about adding an error.
<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 # set to not compress gif, jpg, jpeg, and png image files with the suffix.
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... File for compression
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, such as text/html text/css text/plain.
AddOutputFilterByType DEFLATE application/ms * application/vnd * application/postscript application/javascript application/x-javascript # you only need to know the application/javascript application/x-javascript section., this section indicates compressing javascript files.
AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp # this section tells apache to compress php files.

BrowserMatch ^ Mozilla/4 gzip-only-text/html # Netscape 4.x has some problems, so only the compressed file type is text/html
BrowserMatch ^ Mozilla/4.0 [678] no-gzip # Netscape 4.06-4.08 has more problems, so compression is not enabled.
BrowserMatch bMSIE! No-gzip! Gzip-only-text/html # IE will pretend to be Netscape, but in fact it is no problem
</IfModule>

In this configuration, gzip compression in apache is configured. After apache is restarted, the new configuration will take effect. Of course, apache will not be restarted because the next step is to configure http cache, that is, the mod_expires module.


What are the main differences between mod_gzip and mod_deflate? Which one is better?
The first difference is the difference in the Apache Web server version to install them. There is no built-in web page compression technology in the Apache 1.x series, so we use an additional third-party mod_gzip module to perform compression. Apache 2.x takes the web page compression into consideration when it was officially developed. The mod_deflate module is built in to replace mod_gzip. Although both are Gzip compression algorithms, their operating principles are similar.

The second difference is the compression quality. Mod_deflate compression speed is slightly faster, while mod_gzip compression ratio is slightly higher. By default, mod_gzip is 4% more than mod_deflate ~ 6% of the compression volume.

So why is mod_deflate used? The third difference is the occupation of server resources. In general, mod_gzip requires a higher CPU usage on the server. Mod_deflate is a compression module dedicated to ensuring server performance. mod_deflate requires a small amount of resources to compress files. This means that for high-traffic servers, using mod_deflate may be faster than loading mod_gzip.

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.