Apache Webpage Optimization and Anti-leeching

Table of contents

1. Overview of Apache web page optimization

Two, gzip introduction

Apache Compression Module

Configure web page compression

 1. Check whether the mod_deflate module is installed

 2. Compile and install Apache to add mod_deflate module

 3. Configure the mod_deflate module to enable

 4. Check the installation and start the service 

 5. Test whether mod_deflate compression is effective

3. Web cache

1. Check if the mod_expires module is installed

2. Compile and install Apache and add mod_expires module

3. Configure the mod_expires module to enable

4. Check the installation and start the service

5. Test whether the cache is effective

 4. Hide version information

 5. Apache anti-leech

1. Check whether the mod_rewrite module is installed

2. Compile and install Apache and add mod_rewrite module

3. Configure the mod_rewrite module to enable

4. Web page preparation


1. Overview of Apache web page optimization

In an enterprise, only using the default configuration parameters after deploying Apache will cause many problems on the website. In other words, the default configuration is for the previous low-level server configuration, and the previous configuration is no longer suitable for the current Internet era.
In order to meet the needs of enterprises, it is necessary to consider how to improve the performance and stability of Apache, which is the content of Apache optimization.

 Optimize content:

  • Configure web page compression
  • Configure web caching
  • Configure hidden version number
  • Configure anti-leech

Two, gzip introduction

Configure Apache's webpage compression function, which uses the gzip compression algorithm to compress the webpage content and then transmit it to the client browser

effect:

  • Reduce the number of bytes transmitted by the network and speed up the loading of web pages
  • Save traffic and improve user browsing experience
  • gzip has a better relationship with search engine crawlers

Apache Compression Module

Apache's functional modules for web page compression include

  • mod_gzip module
  • mod_deflate module

Apache 1.x

  • There is no built-in web compression, but the third-party mod_gzip module can be used to perform compression

Apache 2.x

  • During development, the mod_deflate module was built to replace mod_gzip

mod_gzip module and mod_deflate module

  • Both use the gzip compression algorithm and operate similarly
  • mod_deflate compresses slightly faster, while mod_gzip compresses slightly better
  • mod_gzip has a higher CPU usage on the server
  • High traffic servers, using mod_deflate may load faster than mod_gzip

Configure web page compression

1. Check whether the mod_deflate module is installed

apachectl -t -D DUMP_MODULES | grep "deflate"

2. Compile and install Apache to add mod_deflate module

If the mod_deflate module is not installed, recompile and install Apache to add the mod_deflate module

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

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                #加入mod_deflate 模块

make && make install

 3. Configure the mod_deflate module to enable

vim /usr/local/httpd/conf/httpd.conf
--line 52 --modify
Listen 192.198.80.10:80
--line 105 --uncomment
LoadModule deflate_module modules/mod_deflate.so #Open mod_deflate module
--line 197- - Uncomment, modify
ServerName www.test.com:80
-- Add at the end --
<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 #Represents enabling the deflate module to perform gzip compression on the output of this site
</IfModule> 

 

 

4. Check the installation and start the service 

apachectl -t #Verify whether the configuration of the configuration file is correct
apachectl -t -D DUMP_MODULES | grep "deflate" #Check whether the mod_deflate module is installed
  deflate_module (shared) #The correct result of the installation

systemctl start httpd.service

 5. Test whether mod_deflate compression is effective

cd /usr/local/httpd/htdocs
先将game.jpg文件传到/usr/local/httpd/htdocs目录下
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!It works!It works!It works!</h1>
<img src="game.jpg"/>
</body></html>

3. Web cache

1. Check if the mod_expires module is installed

apachectl -t -D DUMP_MODULES | grep "expires"

2. Compile and install Apache and add mod_expires module

If the mod_expires module is not installed, recompile and install Apache to add the 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-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            #加入mod_expires 模块

make && make install 

3. Configure the mod_expires module to enable

vim /usr/local/httpd/conf/httpd.conf
--line 52 --modify
Listen 192.198.80.10:80
--line 111 --uncomment
LoadModule expires_module modules/mod_expires.so #Open mod_expires module
--line 199- - Uncomment, modify
ServerName www.kgc.com:80
-- Add at the end --
<IfModule mod_expires.c>
  ExpiresActive On #Open web page caching function
  ExpiresDefault "access plus 60 seconds" #Set cache for 60 seconds
</IfModule>

4. Check the installation and start the service

apachectl -t #Verify whether the configuration of the configuration file is correct
apachectl -t -D DUMP_MODULES | grep "expires" #Check whether the mod_deflate module has been installed
  deflate_module (shared) #The correct result of the installation

systemctl start httpd.service

5. Test whether the cache is effective

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

 

 4. Hide version information

vim /usr/local/httpd/conf/httpd.conf
--line 491 --uncomment
Include conf/extra/httpd-default.conf

vim /usr/local/httpd/conf/extra/httpd-default.conf
--line 55--Modify
ServerTokens Prod #Change the original Full to Prod, only the name is displayed, no version
#ServerTokens means the response sent by the server to the client Whether the header field contains description information about the server OS type and compiled modules.

systemctl restart httpd.service

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

 

 5. Apache anti-leech

1. Check whether the mod_rewrite module is installed

apachectl -t -D DUMP_MODULES | grep "rewrite"

2. Compile and install Apache and add mod_rewrite module

If the mod_rewrite module is not installed, recompile and install Apache to add the 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\

make && make install

3. Configure the mod_rewrite module to enable

vim /usr/local/httpd/conf/httpd.conf
--157行--取消注释
LoadModule rewrite_module modules/mod_rewrite.so
--224行--
<Directory "/usr/local/httpd/htdocs">
  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted

  RewriteEngine On #Open rewrite function, add mode_rewrite module content
  RewriteCond %{HTTP_REFERER} !^http://accp.com/.*$ [NC] #Set matching rules
  RewriteCond %{HTTP_REFERER} !^http://accp.com $ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.accp.com/.*$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.accp.com/$ [NC]
  RewriteRule . *\.(gif|jpg|swf)$ http://www.accp.com/error.png #Set jump action
</Directory>


 RewriteEngine On                                     
  RewriteCond %{HTTP_REFERER} !^http://accp.com/.*$ [NC]                
  RewriteCond %{HTTP_REFERER} !^http://accp.com$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.accp.com/.*$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.accp.com/$ [NC]
  RewriteRule .*\.(gif|jpg|swf)$ http://www.accp.com/error.png

The field meaning of RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/.*$ [NC]:
"%{HTTP_REFERER}": stores a URL of a link, indicating from which link to access the static content in the directory resource.
"!^": Indicates that it does not start with the following string.
"http://www.kgc.com": It is the path of this website, matched according to the entire string.
".*$": Indicates that it ends with any character.
"[NC]": Indicates case-insensitive letters.

Field meanings of RewriteRule .*\.(gif|jpg|swf)$ http://www.kgc.com/error.png:
".": means match a character.
"*": means to match 0 to multiple characters, combined with "." means to match 0 to multiple preceding any character, if it is 1 to multiple matches, it can be represented by "+".
"\.": "\" here is an escape character, and "\." means the symbol ".". Because "." is a rule character in the command, it 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 rule characters, do the same.
"(gif|jpg|swf)": means match any of "gif", "jpg" and "swf", and "$" means end. The final rule ends with ".gif", ".jpg", and ".swf", preceded by a string of 1 to more characters, that is, a file that matches the image type.
"http://www.kgc.com/error.png": indicates forwarding to this path.

The meaning of the entire configuration is that when using a domain name other than this website to access the image file of this website, the image error.png will be displayed.

4. Web page preparation


Web source host configuration:

cd /usr/local/httpd/htdocs
transfer game.jpg and error.png files to /usr/local/httpd/htdocs directory
vim index.html
<html><body><h1>this is kgc.com!< /h1>
<img src="game. jpg"/>
</body></html>

echo "192.168.154.10 www.accp.com" >> /etc/hosts 
echo "192.168.154.11 www.benet.com" >> /etc/hosts 

Hotlinking website hosts:

cd /usr/local/httpd/htdocs #The default path of the httpd service installed by yum is /var/www/html/
vim index.html
<html><body><h1>this is benet.com!</h1>
< img src="http://www.accp.com/game.jpg"/>
</body></html>

echo "192.168.154.10 www.accp.com" >> /etc/hosts 
echo "192.168.154.11 www.benet.com" >> /etc/hosts 

5. Perform browser verification on the host of Pirate Image website
http://www.benet.com

 

 

 

 

Guess you like

Origin blog.csdn.net/ll945608651/article/details/130117735