在企业中,部署Apache后只采用默认的配置参数,会引发网站很多问题,换言之默认配置是针对以前较低的服务器配置的,以前的配置已经不适用当今互联网时代
为了适应企业需求,就需要考虑如何提升Apache的性能与稳定性,这就是Apache优化的内容。
1.Apache实现网页压缩的功能模块包括
2.Apache 1.X
3.Apache 2.X
4.mod_gzip 模块与mod_deflate 模块
1.启动网页压缩功能步骤
2.检查是否已安装mod_deflate模块
3.若没有安装,则要重新编译安装
4.在配置httpd.conf中配置开启gzip功能
5.重启Apache服务,再用浏览器访问测试网站
6.在浏览器中选择“查看元素”可以看到有"Accept-Encoding:gzip"表示压缩已经生效
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript '设置对什么样的内容启用gzip压缩'
DeflateCompressionLevel '压缩级别'
SetOutputFilter DEFLATE '启用deflate模块对本站点的输出进行gzip压缩'
实验准备
一台centos7做服务器,IP地址为192.168.179.144
一台Windows10做客户机,IP地址为192.168.179.110
相关软件包(fiddler是HTTP的调试抓包工具)
手工编译安装Apache服务
[root@localhost httpd]# tar zxvf apr-1.6.2.tar.gz
[root@localhost httpd]# tar zxvf apr-util-1.6.0.tar.gz
[root@localhost httpd]# tar jxvf httpd-2.4.29.tar.bz2
[root@localhost httpd]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@localhost httpd]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
[root@localhost opt]# yum -y install \
gcc \ '编译器'
gcc-c++ \ '编译器'
make \ 'make工具'
pcre-devel \ '支持正则表达式的工具'
expat-devel \ '使网站能解析标签语言的工具'
perl 'Perl语言工具'
zlib-devel
[root@localhost opt]# cd 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-deflate '开启deflate压缩模块'
> --enable-expires '开启网页缓存功能(后面用到)'
[root@localhost httpd-2.4.29]# make
[root@localhost httpd-2.4.29]# make install
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf'创建软连接方便管理'
编辑配置文件
[root@localhost httpd-2.4.29]# vim /etc/httpd.conf
Listen 192.168.179.144:80
#Listen 80
ServerName www.cllt.com:80
...
LoadModule headers_module modules/mod_headers.so '这三条全部开启'
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so
...
<IfModule mod_deflate.c> '在末尾加入'
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascrip text/jpg text/png
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
</IfModule>
[root@localhost httpd-2.4.29]# /usr/local/httpd/bin/httpd -t '使用apachectl工具检查是否配置正确'
Syntax OK
启动服务
[root@localhost httpd-2.4.29]# setenforce 0
[root@localhost httpd-2.4.29]# iptables -F
[root@localhost httpd-2.4.29]# /usr/local/httpd/bin/httpd '开启服务'
[root@localhost httpd-2.4.29]# netstat -natp | grep 80
tcp 0 0 192.168.179.144:80 0.0.0.0:* LISTEN 42685/httpd
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/bin/
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES |grep "deflate" '查看模块'
deflate_module (shared)
[root@localhost htdocs]# ls '添加一张名为hua的图片'
index.html hua.jpg
[root@localhost htdocs]# vim index.html
<html><body><h1>It works!</h1>
<img src="hua.jpg"/>
</body></html>
在Windows10中安装fiddler软件
使用fiddler工具抓包
1.启用网页缓存功能步骤
2.查看是否安装mod_expire模块
3.如果没有安装,需要重新编译安装
4.修改httpd.conf配置文件
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
</IfModule>
5.重启Apache服务,再用浏览器访问测试网站
6.在浏览器中选择“查看元素”可以看到有”Expires“项表示缓存生效
'配置Apache服务时需要增加缓存功能,上面做网页压缩时已经添加过'
[root@localhost httpd-2.4.29]# ./configure \
--enable-expires
编辑配置文件
[root@localhost opt]# vim /etc/httpd.conf
LoadModule expires_module modules/mod_expires.so '开启模块'
...
<IfModule mod_expires.c> '配置文件末尾添加'
ExpiresActive On
ExpiresDefault "access plus 50 seconds" '设置缓存时间50秒'
</IfModule>
进行测试
[root@localhost opt]# cd /usr/local/httpd/bin/
[root@localhost bin]# ./apachectl -t '测试配置文件是否正确'
Syntax OK
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "expires" '查看模块'
expires_module (shared)
[root@localhost bin]# ./apachectl restart '重启Apache服务'
[root@localhost bin]# vim /etc/httpd.conf
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 300 seconds" '修改为300秒'
</IfModule>
[root@localhost bin]# ./apachectl restart
1.使用三台主机模拟盗链及防盗链实验
源主机:域名www.cllt.com,IP地址192.168.179.144
盗链网站:IP地址192.168.179.10
客户端:Windows10
2.检查Apache是否安装了mod_rewrite模块
3.配置规则变量说明
4.规则匹配说明
5.配置文件
RewriteEngine On
RewriteCond %{HTTP_REFERER}!^http://test.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://test.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.test.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.test.com$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.test.com/error.png
1.配置DNS服务
[root@localhost ~]# yum -y install bind
[root@localhost ~]# vim /etc/named.conf
options {
listen-on port 53 { any;};
...省略内容
allow-query { any; };
[root@localhost ~]# vim/etc/named.rfc1912.zones
zone "cllt.com" IN {
type master;
file "cllt.com.zone";
allow-update { none; };
};
[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost cllt.com.zone
[root@localhost named]# vim cllt.com.zone
www IN A 192.168.179.144
[root@localhost named]# iptables -F
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start named
盗链网站配置httpd服务
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.179.10:80
#Listen 80
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.html
<h1>this is test web</h1>
<img src="http://www.cllt.com/hua.jpg"/> '指向源服务器的图片'
[root@localhost html]# iptables -F
[root@localhost html]# setenforce 0
[root@localhost html]# systemctl start httpd.service
[root@localhost bin]# vim /etc/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so '去掉注释'
...
<Directory "/usr/local/httpd/htdocs">
...
RewriteEngine On '添加此6行内容''表示如果输入的域名不是前面4种,而是通过其他链接网页跳转过来的,则匹配最后一条规则'
RewriteCond %{HTTP_REFERER} !^http://cllt.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://cllt.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.cllt.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.cllt.com$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.cllt.com/error.png
</Directory>
[root@localhost bin]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
error.png hua.jpg index.html '添加配置文件中的error图片'
[root@localhost htdocs]# cd ..
[root@localhost httpd]# cd bin
[root@localhost bin]# ./apachectl stop
[root@localhost bin]# ./apachectl start
修改配置文件
[root@localhost bin]# vim /etc/httpd.conf
...
Include conf/extra/httpd-default.conf '将此行注释去掉'
[root@localhost bin]# cd /usr/local/httpd/conf/extra
[root@localhost extra]# vim httpd-default.conf
ServerTokens Prod 'Server Tokens Full修改为Server Tokens Prod'
[root@localhost extra]# cd /usr/local/httpd/bin/
[root@localhost bin]# ./apachectl stop
[root@localhost bin]# ./apachectl start