Improve the speed of the application ----------------- Webpage optimization (compression)

Why do web pages need to be compressed?

The access speed of the website is determined by a number of factors, including the response speed of the program, network bandwidth, server performance, and network transmission speed between the client and the client. The use of compression can increase the speed of the application, and the key is that it does not require any cost, but it only increases the server CUP share by one or two percentage points or less.

The specific operation is as follows
(1) Check whether the mod_deflate module is installed

[root@localhost ~]# apachectl -D DUMP_MODULES | grep "deflate"

(2) Install the mod_deflate module.
If the mod_deflate module is not installed, you need to stop the Apache service, recompile and install Apache,
and add the mod_deflate module content to the parameters

[root@localhost ~]# systemctl stop httpd
[root@localhost~]# cd /opt/httpd-2.4.29/

[root@localhost httpd-2.4.29]#
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-cgid \
--enable-deflate

[root@localhost httpd-2.4.29]# make && make install

**Note:** Sometimes when compiling and installing Apache, the following error will appear:
checking whether to enable mod_deflat…configure: error: mod_deflate has been
requested but can not be built due to prerequisite failures

solution:

[root@localhost~]# yum install -y zlib-devel

(3) Configure mod_deflate module to be enabled After
compilation and installation, mod_deflate module needs to be enabled in httpd.conf file to take effect.

[root@localhost httpd-2.4.29]vi /usr/local/httpd/conf/httpd.conf

LoadModule deflate_module modules/mod_deflate.so   #把前面“#”去掉
在文本末尾加入以下内容
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript image/png image/jpg
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
</IfModule>

(4) Check httpd.conf syntax

[root@localhost httpd-2.4.29]# httpd -t
Syntax OK

problem:

[root@localhost httpd-2.4029]# httpd -t  	   	##当出现如下问题就是需要优化路径
-bash: httpd: command not found

Refer to this article to optimize and
optimize the execution path

(5) Test
First upload the b.jpg photo to /usr/local/httpd/htdocs/

[root@localhost httpd-2.4.29]# cd /usr/local/httpd/htdocs/

[root@localhost htdocs]# vi index.html   		 ##编辑测试页
----测试页-----
<html>
<head>
<title>--压缩测试页--</title>
</head>
<body><h1>这是一个测试网页内容压缩的页面!!This is test Pagell</h1>
<img src=b.jpg / >
</body>
</html>

Problem: When there are garbled characters in the test page, the solution is as follows

[root@localhost ~]# vi /etc/httpd.conf
AddDefaultCharset utf-8						#插入这条命令

[root@localhost ~]# systemctl restart httpd

After the compression is completed, you can use wireshaerk to capture packets to test whether the image transmission is compressed.
Seeing the following similar messages indicates success

HTTP/1.1 200 OK (JPEG JFIF image)
Hyertext Transfer Protocol
HTTP/1.1 200 OK \r \n
content-Encoding: gzip \r \n 		###这个地方表示图片压缩###

Guess you like

Origin blog.csdn.net/weixin_48190875/article/details/108644113