Enable gzip compression for apache in CentOS

Source: Internet
Author: User
Tags website performance

Enable gzip compression for apache in CentOS
1. gzip Introduction

Gzip is a popular File compression algorithm. It is widely used, especially on Linux platforms. When Gzip is applied to a plain text file, the effect is obvious, which can reduce the file size by more than 70%. This depends on the content of the file.

Using the Gzip module in Apache, we can use the Gzip compression algorithm to compress the webpage content published by the Apache server and then transmit it to the client browser. This actually reduces the number of bytes transmitted over the network after compression. The most obvious advantage is that it can speed up web page loading.

The advantage of accelerated webpage loading is self-evident. In addition to saving traffic and improving the user's browsing experience, Gzip has a better relationship with search engine crawling tools. For example, Google can directly read gzip files to retrieve webpages faster than normal manual crawlers. As you can see in Google Webmaster toolsgz, sitemap.xml.gz is directly submitted as Sitemap.

These benefits are not limited to static content. PHP dynamic pages and other dynamically generated content can be compressed by using the Apache compression module, adding other performance adjustment mechanisms and corresponding server-side cache rules can greatly improve the website performance. Therefore, for PHP programs deployed on Linux servers, we recommend that you enable Gzip Web compression with server support.

2. 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.

The following are two demo images:

Gzip not used:

3. Let's get started!

Apache uses the Gzip compression algorithm to compress modules: mod_gzip and mod_deflate. To use Gzip Web compression, first make sure that your server has enabled support for one of the two components. On Linux servers, more and more space vendors are now enabling their support, and some even support both modules at the same time. For example, currently, the servers of Space vendors such as Godaddy, Bluehost, and DreamHosts support both mod_gzip and mod_deflate.

Although Gzip also requires support from the client browser, do not worry. Currently, most browsers support Gzip, such as IE, Mozilla Firefox, Opera, and Chrome.

By viewing the HTTP header, we can quickly determine whether the client browser can accept gzip compression. If the following information appears in the HTTP header, your browser supports gzip compression:

Accept-Encoding: gzip supports mod_gzipAccept-Encoding: deflate supports mod_deflate Accept-Encoding: gzip, deflate supports both mod_gzip and mod_deflate

If the server enables support for Gzip components, you can customize them in http. conf or. htaccess. Below is a simple example of. htaccess Configuration:

# mod_gzip:<ifModule mod_gzip.c>mod_gzip_on Yesmod_gzip_dechunk Yesmod_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>
# Mod_deflate: <ifmodule mod_deflate.c> DeflateCompressionLevel 6 # compression rate. 6 is the recommended value. addOutputFilterByType DEFLATE text/includeflate application/xhtml + includeflate application/rss + includeflate application/ atom_xmlAddOutputFilterByType DEFLATE application/x-javascriptAddOutputFilterByType DEFLATE application/x-httpd-phpAddOutputFilterByType DEFLATE image/svg + xml </ifmodule>

The file MIME type can be added according to your own situation. As for PDF files, images, music files, and other files, these files are already highly compressed, and repeated compression does not play a major role, however, the performance may be reduced by increasing the CPU processing time and rendering of the browser. Therefore, it is unnecessary to use Gzip compression. After the preceding settings, check the returned HTTP header. If the following information is displayed, the returned data has been compressed. That is, the Gzip compression configured by the website program takes effect.

Content-Encoding: gzip

Note: No matter whether mod_gzip or mod_deflate is used, the information returned here is the same. Because they are all implemented gzip compression methods.

In addition, you can also use some online detection tools (such as: http://www.whatsmyip.org/http_compression/) to detect whether your website content has been compressed by Gzip.

4. 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.

Not quite clear? In short, if your website has less than 1000 independent visitors every day and you want to speed up webpage loading, use mod_gzip. Although it will consume some additional server resources, it is also worthwhile. If your website has more than 1000 independent visitors each day and you are using a shared virtual host and the allocated system resources are limited, using mod_deflate will be a better choice.

In addition, from Apache 2.0.45, mod_deflate can use the DeflateCompressionLevel command to set the compression level. The value of this command can be an integer between 1 (the fastest compression speed and the lowest compression quality) and 9 (the slowest compression speed and the highest compression ratio, the default value is 6 (the compression speed is balanced with the compression volume ). This simple change makes mod_deflate much easier than mod_gzip compression.

P.S. for virtual spaces without the preceding two Gzip modules enabled, you can use the zlib function library of php (also check whether the server supports this function) to compress files, this method is troublesome to use and generally consumes server resources. Please use it with caution as needed.

V. Effect

Before enabling:


After enabling

Link: http://bbs.linuxtone.org/home-space-uid-11671-do-blog-id-295.html

Related Article

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.