How to optimize Apache pages

Web page compression

1. Check whether mod is installed_ Deflate module

apachectl -t -D DUMP_MODULES| grep "deflate"

2. If mod is not installed_ Deflate module, recompile and install Apache, add mod_deflate module

systemctl stop httpd.service

cd /usr/local/httpd/conf
mv httpd.conf httpdno.conf.bak
yum -y install gcc gcc-c++ pcre pcre-devel zlib-deve
#Add mod deflate module

make -j2 && make install

3. Configure Mod_ Enable module def

vim /usr/local/httpd/conf/httpd.conf
--52 that 's ok--modify
Listen 192.168.154.16:80

--105 that 's ok--note off
LoadModule deflate module modules/mod deflate. so .
#Enable mod_deflate module
--197 that 's ok--Uncomment, modify
Serve rName www.kgc.com: 80

--Last line add--
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png 		#Represents what kind of content to enable gzip compression
DeflateCompressionLevel 9   	#Represents the compression level, ranging from 1 to 9
SetOutputFilter DEFLATE   	 #On behalf of enabling deflate module to gzip compress the output of this site
</IfModule>

4. Check the installation and start the service

apachectl -t

#Verify that the configuration file is configured correctly
apachectl -t -D DUMP_MODULES | grep "deflate"
#Check Mod_ Is deflate module installed

deflate module (shared)
#Installed correct results

systemctl start httpd.service

5. Test Mod_ Whether deflate compression takes effect

cd /usr/local/httpd/htdocs
 123 first.png File transfer/usr/local/httpd/htdocs Under the directory

vim index.html
<html>
<body>
<h1>
It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!Itworks!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works! It
works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works! It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!works!
</h1>
<img src="123.png"/>
</body>
</html>

6. Verification
In Linux system, open Firefox browser and right-click to view elements
Select network - > select HTML, WS, other
visit http://192.168.154.16 , double-click the 200 response message to see that the response header contains content encoding: gzip

Method 2:
Install microsoft.com successively in Windows system Net4 and fiddler software, open Fiddler software
Select inspectors - > select Headers
Browser access http://192.168.154.16 , double-click the 200 response message to view the content encoding: gzip

Web cache

1. Check whether it is installed
mod expires module

apachectl -t -D DUMP_MODULES | grep "expires"

2. If mod is not installed_ Expires module, recompile and install Apache, add mod_ expires module

systemctl stop httpd.service
cd /usr/local/httpd/conf

mv httpd.conf httpd.conf.bak1
yum -y install gcc gcc-c++ pcre pcre-devel zlib-deve

cd /opt/httpd-2.4.29/
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate \
--enable-expires

#Add mod expires module

make -j2 && make install

3. Configure mod_ expires module enabled

vim /usr/local/httpd/conf/httpd.conf
--52 that 's ok--modify
Listen 192.198.154.16:80
--111 that 's ok--note off
LoadModule expires_ module modules/mod_ expires. so
#Enable mod_ expires module

--199 that 's ok--Uncomment, modify
ServerName www.kgc.com: 80

--Last line add--
<IfModule mod_expires.c>
  ExpiresActive On        					#Turn on Web page caching
  ExpiresDefault "access plus 60 seconds"  	#Set cache for 60 seconds
</IfModule>

4. Check the installation and start the service

apachectl -t

#Verify that the configuration file is configured correctly
apachectl -t -D DUMP_MODULES | grep "expires"
#Check that the mod deflate module is installed
deflate module (shared)
#Installed correct results
systemctl start httpd.service

5. Test whether the cache is effective

cat /usr/local/httpd/htdocs/index.html

Method 1:
In Linux system, open Firefox browser and right-click to view elements
Select network - > select HTML, WS, other
visit http://192.168.154.16 , double-click the 200 message to see that the response header contains the Expires item

Method 2:
Install microsoft.com successively in Windows system Net4 and fiddler software, open Fiddler software
Select inspectors - > select Headers
Browser access http://192.168.154.16 , double-click the 200 message to view the Expires item

Hide version information

vim /usr/local/httpd/conf/httpd.conf
--491 that 's ok--note off
Include conf/extra/httpd-default. conf

vim /usr/local/httpd/conf/extra/httpd-default.conf
--55 that 's ok--modify
ServerTokens Prod
#Change the original Full to Prod, only display the name, no version
#ServerTokens indicates whether the response header field returned by the Server to the client contains information about the Server OS type and the compiled module description.
systemctl restart httpd. service

Browser access http://192.168.154.16 , double-click the 200 message to view the Server item

Apache anti-theft chain

1. Check whether mod rewrite module is installed

apachectl -t -D DUMP_MODULES | grep "rewrite"

2. If mod is not installed_ Rewrite module, recompile and install
Apache add mod__ Rewrite Module

systemctl stop httpd. service

cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak2
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel

cd /opt/httpd-2.4.29/

./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate \
--enable-expires
#Add mod rewrite Module

make -j2 && make install

3. Configure mod_ rewrite Module enabled

vim /usr/local/httpd/conf/httpd.conf

--157 that 's ok--note off
LoadModule rewrite_ module modules/mod_ rewrite. so
-224 that 's ok--
<Directory "/usr/local/httpd/htdocs">
  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted

 RewriteEngine On              #Open the rewrite function and add mode_rewrite module content
  RewriteCond %{HTTP_REFERER} !^http://kgc.com/.*$ [NC] # set matching rules
  RewriteCond %{HTTP_REFERER} !^http://kgc.com$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/.*$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/$ [NC]
  RewriteRule .*\.(gif|jpg|swf)$ http://www.kgc.com/error.png # set Jump Action
</Directory>

RewriteCond tells {HTTP reference}^ http:/ /www. kgc. com/.$ Field meaning of [NC]:
"Each {httpreferrer}": the URL that stores a link, indicating which link to access the required web page from.
"! ^": indicates that it does not start with the following string.
" http://www. kgc. com ": is the path of this website, matching according to the whole string.
“.KaTeX parse error: Can't use function '\.' in math mode at position 48: … RewriteRule .*\ ̲.̲ (gifljpg|swf) http:/ /www.kgc. com/error. Meaning of PNG field:

"." means to match one character.
"*" means to match 0 to more than one character, which is the same as "." Together, it means to match any character in front of 0 to multiple times. If it is 1 to multiple times, it can be represented by "+".
“.”: The "\" here is the escape character, " "Represents the meaning of the symbol". "Because". "Is a regular character in the instruction and has a corresponding meaning,
If you need to match, you need to add an escape character "\" in front of it. If you need to match other regular characters, do the same.
"(gifljpg|swf)": means to match any one of "gif", "jpg" and "SWF", and "$" means to end. The final rule is ". gif", ". jpg", " Swf "end,
It is preceded by a string of 1 to more than one character, that is, a file matching the image type.
"Http: / / www.kgc. COM / error. PNG": indicates forwarding to this path.
The meaning of the whole configuration is to display error when accessing the image file of this site with a website domain name other than this site Png this picture.

Keywords: Linux Apache

Added by maqmus on Wed, 02 Feb 2022 10:22:25 +0200