apache 网页优化

 

Apache 优化

一: Apache 网页优化

1.1 配置网页压缩功能

apache 实现网页压缩的功能模块有 mod_gzip 模块 和 mod_deflate模块。

  • 在Apache 1.X 没有内建网页压缩技术,但是可以使用第三方mod_gzip 模块执行压缩

  • 在Apache 2.X 内建了 mod_deflate 这个模块,取代了mod_gzip

  • 高流量的服务器,使用 mod_deflate 可能比mod_gzip 加载速度快

 mod_gzip模块mod_deflate模块
压缩算法 使用 gzip压缩算法 使用gzip压缩算法
压缩速度 略低
压缩比 略低
cpu占比 略高

 

1.1.1 检查 是否安装了mod_deflate模块

 apachectl -t -D DUMP_MODULES | grep "deflate"  
 #如果输出中没有 deflate_module(share) ,说明编译时候没有安装mod_deflate 模块,需要重新编译安装

 

img

 

1.1.2 配置mod_deflate 模块启用

 yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
 systemctl stop httpd
 #先停止服务
 netstat -natp | grep httpd
 mv /usr/local/httpd/conf/httpd.conf httpd.conf.bak2  
 #将原来的配置文件移走
 ​
 ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-deflate   
 #重新编译安装,编译时候加上 --enable-deflate,添加od_deflate 模块
 make && make install
 ​
 vim /usr/local/httpd/conf/httpd.conf
 Listen 192.168.23.13:80
 ServerName www.mynet.com
 ​
 LoadModule deflate_module modules/mod_deflate.so  
 #大概105行,打开注释,启用deflate模块
 ​
 #文件末行添加
 <IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png 
 DeflateCompressionLevel 9   
 SetOutputFilter DEFLATE    
 </IfModule>
 ​
 ​
 #DeflateCompressionLevel 9   #代表压缩级别,范围为1~9
 #SetOutputFilter DEFLATE    #代表启用deflate 模块对本站点的输出进行gzip压缩

 

 

1.1.3 检查安装情况,启动服务

 systemctl restart httpd
 netstat -natp | grep :80 
 ​
 apachectl -t -D DUMP_MODULES | grep "deflate"

 

image-20210811134515694

 

1.1.4 编写网页文件

 cd /usr/local/httpd/htdocs/
 vim /usr/local/httpd/htdocs/index.html   #网页文件里多写点内容,防止应为大小不够而不进行压缩

 

image-20210811212215682

 

1.1.5 测试

 firefox http://192.168.23.13

 

image-20210811215950969

 

 

1.2 配置网页缓存

通过mod_expire 模块配置Apache ,使网页能在客户端浏览器缓存一段时间,以避免重复请求

启用mod_expire 模块后,会自动生成页面头部信息中的Expires 标签和 Cache-Control标签,客户端浏览器根据标签决定下次访问是在本地机器的缓存中获取页面,不需要向服务器再次发出请求,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的

 

1.2.1 检查是否安装了mod_expires模块

  apachectl -t -D DUMP_MODULES | grep "expires"

 

image-20210811213528895

 

1.2.2 重新编译安装apache ,添加 mod_expiresn模块

 systemctl stop httpd
 netstat -natp | grep :80
 cd /usr/local/httpd/conf
 mv httpd.conf httpd.conf.bak3
 ​
 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-expires   
 #编译时加上--enable-expires,添加mod_expires模块
 ​
 make && make install 
 

 

1.2.3 配置mod_expires 模块启用

 vim /usr/local/httpd/conf/httpd.conf
 Listen 192.168.23.13:80
 ServerName www.mynet.com:80
 ​
 #打开注释,启用mod_expires模块
 LoadModule expires_module modules/mod_expires.so   
 ​
 #配置文件末尾添加
 <IfModule mod_expires.c>
   ExpiresActive On     #打开网页缓存功能            
   ExpiresDefault "access plus 60 seconds"     #缓存60s  
 </IfModule>
 

 

1.2.4 检查安装情况,启动服务

 apachectl  -t
 apachectl -t -D DUMP_MODULES | grep "expires"
 systemctl restart httpd

 

image-20210811214607294

 

1.2.5 访问测试

 firefox http://192.168.23.13
 #要清空浏览器缓存

 

image-20210811215212192

 

 

二: Apache 安全优化

2.1 隐藏版本信息

Apache 的版本信息,透露了一定的漏洞消息,从而给网站带来安全隐患,需要隐藏版本信息

image-20210811220341759

 

2.1.1 修改配置文件,隐藏版本信息

 vim /usr/local/httpd/conf/httpd.conf   #修改主配置文件
 Include conf/extra/httpd-default.conf   #加载辅助配置 extra/http-default.conf
 ​
 vim /usr/local/httpd/conf/extra/httpd-default.conf 
 #在55行,将ServerTokens Full 改为 ServerTokens Prod ,只显示名称,不显示版本号
 ServerTokens Prod   
 #ServerTokens 表示 server 回送给客户端响应头是否包含关于服务器OS类型和编译过的模块信息

 


 

2.1.2 重启服务,并访问测试

 systemctl restart httpd
 firefox  http://192.168.23.13
 #要先清除浏览器缓存

 

image-20210811221825490

 

 

2.2 配置防盗链

  • 防盗链时繁殖别人的网站代码里盗用我们自己服务器上的图片,文件,视频等相关资源

  • 如果别人盗用网站的这些静态资源,明显会增大服务器带宽压力,需要杜绝网站静态资源被他人盗用

 

2.2.1 检查是否安装mod_rewrite 模块

 apachectl -t -D DUMP_MODULES | grep "rewirte"

 

image-20210811222214079

 

2.2.2 没有安装模块,则重新编译安装,在编译时加入mod_rewrite 模块

 systemctl stop httpd
 netstat -natp | grep :80
 mv conf/httpd.conf conf/httpd.conf.bak4
  cd /opt/httpd-2.4.29/
 ​
 ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-deflate --enable-expires --enable-rewrite   
 #编译时加上--enable-rewrite ,添加mod_rewrite模块
 ​
 make && make install 

 


 

 

2.2.3配置mod_rewrite模块启用

 cd /usr/local/httpd
 ​
 vim conf/httpd.conf
 Listen 192.168.23.13:80
 ServerName www.mynet.com:80
 LoadModule rewrite_module modules/mod_rewrite.so   
 #大概在157行,打开注释,开启mod_rewrite 模块
 ​
 <Directory "/usr/local/httpd/htdocs">  #大致在224行
 Options Indexes FollowSymLinks   #保持此配置不变
 AllowOverride None      #保持此配置不变
 Require all granted     #保持此配置不变
 ​
 RewriteEngine On             #打开rewrite 功能,加入mod_rewrite模块内容
 ​
 RewriteCond %{HTTP_REFERER} !^http://mynet.com/.*$ [NC]  #设置规则匹配
 RewriteCond %{HTTP_REFERER} !^http://mynet.com$ [NC]
 RewriteCond %{HTTP_REFERER} !^http://www.mynet.com/.*$ [NC]
 RewriteCond %{HTTP_REFERER} !^http://www.mynet.com/$ [NC]
 ​
 RewriteRule .*\.(gif|jpg|swf)$ http://www.mynet.com/error.png  #设置跳转动作 
 #使用本网站以外的域名访问本站如图片,则显示 error.png  这个图片
 </Directory>
 ​
 systemctl restart httpd  #重启httpd 服务
 ​
 #RewriteCond %{HTTP_REFERER} !^http://mynet.com/.*$ [NC]  含义
 #"%{HTTP_REFERER}" 存放一个链接的URL,表示从那个链接访问所需的网页
 #"!^" 表示不以后面的字符串开头
 # "http://mynet.com"  本网站的路径,按整个字符串匹配
 # “.$”  以任意字符结尾
 #[NC] 不区分大小写字母
 ​
 # RewriteRule .*\.(gif|jpg|swf)$ http://www.kgc.com/error.png含义
 # ” .* " 匹配前面任意字符任意次数
 #  "\. "   \转移字符, “ \." 表示 仅为点.  ,不含其他意思
 #" (gif|jpg|swf)$ "   表示gif或者jpg或者swf 结尾的,也就是图片的格式
 #  http://www.mynet.com/error.png    #转发到这个路径

 

image-20210811225333278

 

2.2.4 网页装备

web 源主机:

 cd /usr/local/httpd/htdocs/
 #将图片和 error.png 图片传到这个目录下
 <html><body>
 <h1>
 this is mynet web
 </h1>
 <img src="girl.jpg">
 </body></html>
 echo "192.168.23.13 www.myweb.com" >> /etc/hosts
 echo "192.168.23.12 www.benet.com" >> /etc/hosts

 

 

盗链网站主机:

 echo "192.168.23.13 www.mynet.com" >> /etc/hosts
 echo "192.168.23.12 www.benet.com" >> /etc/hosts
 ​
 yum -y install httpd
  cd /var/www/html/
  
  vim index.html
 <html><body><h1>this is benet.com</h1>
 <img src="http://www.mynet.com/girl.jpg"/>
 </body></html> 
  systemctl restart httpd
 

 

 

客户端访问测试

 echo "192.168.23.13 www.mynet.com" >> /etc/hosts
 echo "192.168.23.12 www.benet.com" >> /etc/hosts
 ​
 firefox http://www.mynet.com
 firefox http://www.benet.com

 


 

image-20210811232112190

 

 

image-20210811233932847

 

posted @ 2021-08-15 06:07  知己一语  阅读(60)  评论(0编辑  收藏  举报