Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to enable gzip Compression Service in apache

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Setting gzip compression on the server is a common practice in web development. Suppose you want to request a 100k file with a network transfer speed of 50k/s, and it takes 2s to get the data. However, if gzip compression is set on the server, the file on the server is compressed to 50k (the actual compression ratio is often less than 50%). In this case, it only takes 1 second to get the data, and then decompress it on the client.

You can compare the size of the same file before and after opening gzip.

Before gzip compression:

After gzip compression:

So how to start the gzip service on the server side? Here is a brief introduction to apache as an example.

Open the "httpd.conf" file of apache, for example, mine is in the "C:\ wamp\ bin\ apache\ Apache2.2.21\ conf" directory. Find the following line and remove the comment (#) in front of it:

Copy the code LoadModule deflate_module modules/mod_deflate.so

Many reference documents mention the need to uncomment LoadModule headers_module modules/mod_headers.so at the same time, saying, "if you don't open this, the site won't display properly," but I didn't remove it during the test.

Then add the following code:

Copy code # tells apache to compress the content transferred to the browser SetOutputFilter DEFLATE# compression level 9DeflateCompressionLevel 9

In this way, all files can be compressed by gzip. The compression level is an integer between 1 and 9, with values ranging from 1 (lowest) to 9 (highest). It is not recommended to set too high, although it has a high compression ratio, it takes up more CPU resources. (there is not much difference between the next 1 and 9 compression ratios tested locally.)

In actual development, we do not need to compress all files, for example, we do not need to compress the image file with gzip, because the image file (usually in jpg, png, etc.) has already been compressed, and then gzip compression may be counterproductive (for more information, do you want to enable gzip compression for pictures? Absolutely not! Do not compress background images with gzip, especially PNG), as well as PDF and music files. So we can set up to filter the specified file or compress the specified file.

For example, we need to avoid gzip compression for special files such as images:

Copy code # tells apache to compress the content transferred to the browser SetOutputFilter DEFLATE# compression level 9DeflateCompressionLevel compression setting does not compress the picture file with the suffix gif,jpg,jpeg,png SetEnvIfNoCase Request_URI. (?: gif | jpe?g | png) $no-gzip dont-vary

Or specify a file format to compress:

Copy code # compression level 9DeflateCompressionLevel compression types html, xml, php, css, jsAddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-javascript application/x-httpd-phpAddOutputFilter DEFLATE js css

After the modification, save the httpd.conf file, remember to restart apache, and then refresh the browser to see the request, it should have taken effect!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report