web服务器学习4---httpd-2.4.29优化配置

实验环境:

环境:CentOS 7.4

软件版本:httpd-2.4.29

一.网页压缩

apached的网页压缩可以减少服务器流量,提示服务器性能,使用mod_deflate模块实现.

1.检查是否安装压缩模块

apachectl -D DUMP_MODULES | grep deflate

如果没有需要重新编译安装apache

./configure
--prefix=/usr/local/httpd
--enable-so
--enable-rewrite
--enable-charset-lite
--enable-cgi
--enable-cgid
--enable-deflate

make && make install

修改配置文件,启用模块

vi /usr/local/httpd/conf/httpd.conf

LoadModule deflate_module modules/mod_deflate.so

httpd -t

service httpd restart

apachectl -D DUMP_MODULES | grep deflate

2.修改配置文件,添加压缩配置

vi /usr/local/httpd/conf/httpd.conf

AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript image/png image/jpeg application/x-httpd-php application/x-javascript
DeflateCompressionLevel 9
SetOutputFilter DEFLATE

AddDefaultCharset utf-8

标注支持压缩的格式

压缩的级别
代表启用deflate模块对本站点进行压缩

解决中文乱码

service httpd restart

3.创建测试网页

vi /var/test2/index.html

<html>
<head>
<title>--压缩测试页--</title>
</head>
<body><h1>这是test2网页内容压缩的页面!!This is test Page!!</h1>
<img src=test.jpg / >
</body>
</html>

//虚拟主机需要将图片放置在虚拟主机存放文档的目录,例如/var/test2;
非虚拟主机直接放在/httpd/htdocs/

4.测试截图及看报文对比

日志分析:

已经开启压缩

 未开启压缩

二. 网页缓存

网页缓存可以把网页缓存在客户端,对于不经常更新的页面客户端不需要再向服务器发出请求,能节省流量,使用mod_expires模块实现。

1.检查是否安装mod_expires模块

apachectl -D DUMP_MODULES | grep expires

2.重新编译安装

./configure
--prefix=/usr/local/httpd
--enable-so
--enable-rewrite
--enable-charset-lite
--enable-cgi
--enable-cgid
--enable-deflate
--enable-expires

make && make install

3.修改配置文件

vi /usr/local/httpd/conf/httpd.conf

LoadModule expires_module modules/mod_expires.so                      //启用缓存模块

<IfModule mod_expires.c>                                                                  //开启缓存
ExpiresActive On
ExpiresDefault "access plus 60 seconds"                                           //缓存设置60秒
</IfModule>

service httpd start

4.实验验证

 三.防盗链

可以防止其他网站使用本网站的资源,避免不必要的流量开销,使用mod_rewrite模块实现。

1.实验准备

源主机  192.168.80.180

盗链主机  192.168.80.80

盗链主机能够解析源主机的域名

vi /etc/hosts

192.168.80.180 www.test2.com

2.源主机编辑网页

vi /var/test2/index.html

<html>
<head>
<title>--合法主机--</title>
</head>
<body><h1>这是192.168.80.180合法主机!</h1>
<img src=test.jpg / >
</body>
</html>

盗链网页

vi /usr/local/httpd/htdocs/index.php

<html>
<title>盗链主机</title>
<body>
<h1> 伸手拿来</h1>
<img src="http://www.test2.com/test.jpg">
</body>
</html>

3.实验截图 

4.设置防盗链

4.1需要mod_rewrite模块支持

vi /etc/httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so

4.2配置mod_rewrite模块启用

<Directory "/var/test2/">
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://192.168.80.180/*
RewriteCond %{HTTP_REFERER} !^http://test2.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://test2.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.test2.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.test2.com$ [NC]
RewriteRule .*.(gif|jpg|swf|png)$ http://www.test2.com/doc/error.jpg [R,NC]
</Directory>

脚本的意思:不以。。。。开头的链接都重定向到http://www.test2.com/doc/error.jpg

“ %{HTTP_REFERER} ” :表示从哪个url来产生的链接;

‘’  !^ ‘’:表示不以。。。开头;

“ .*$ ” :表示以任意字符结尾;

“ [NC] ” :表示不区分大小写;

4.3在合法主机上创建一个虚拟目录,并使盗链主机可以访问 

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>

4.4在虚拟目录里放置错误图片

  cp /var/test2/error.jpg /usr/share/doc/

4.4 访问盗链主页,实验截图

 四. 隐藏版本信息

隐藏版本信心可以避免针对版本的信息的漏洞共计.

1.主配置文件开启httpd-default

vi /etc/httpd.conf

Include conf/extra/httpd-default.conf               //去掉注释

2.修改httpd-default

cd /usr/local/httpd/conf/

vi extra/httpd-default.conf

ServerTokens Prod
Serversignature Off

service httpd restart

3.抓包对比

  

 ServerTokens的输出格式:

原文地址:https://www.cnblogs.com/youxxn/p/8686357.html