Apache网页优化概述
优化内容
Apache实现网页压缩的功能模块包括:
Apache 1.x
没有内建网页压缩技术,但可使用第三方mod_ _gzip 模块执行压缩
Apache 2.x
在开发的时候,内建了mod_deflate 这个模块,取代mod_gzip
mod_gzip 模块与mod_deflate 模块
首先检查是否安装了mod_deflate模块
若没有安装则要重新编译安装
#首先挂载软件包并进行解压
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.17.1/share /abc
Password for root@//192.168.17.1/share:
[root@localhost ~]# cd /abc
[root@localhost abc]# ls
apr-1.6.2.tar.gz cronolog-1.6.2-14.el7.x86_64.rpm httpd-2.4.29.tar.bz2 LAMP.txt
apr-util-1.6.0.tar.gz Discuz_X2.5_SC_UTF8.zip httpd-2.4.2.tar.gz mysql-5.6.26.tar.gz
awstats-7.6.tar.gz extundelete-0.2.4.tar.bz2 john-1.8.0.tar.gz php-5.6.11.tar.bz2
[root@localhost abc]# tar zxvf apr-1.6.2.tar.gz -C /opt
[root@localhost abc]# tar zxvf apr-util-1.6.0.tar.gz -C /opt
[root@localhost abc]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt
[root@localhost abc]# cd /opt
[root@localhost opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@localhost opt]# ls
httpd-2.4.29 rh
[root@localhost opt]# cd httpd-2.4.29/
#安装语言包
[root@localhost httpd-2.4.29]# yum install -y gcc gcc-c++ pcre-devel pcre expat-devel
#执行安装功能模块
[root@localhost httpd-2.4.29]#./configure \
--prefix=/usr/local/httpd \
--enable-so --enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate
[root@localhost httpd-2.4.29]# make && make install
#在配置httpd.conf中配置开启gzip功能
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/conf
[root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# ln /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
[root@localhost conf]# vim /etc/httpd.conf //进入配置文件
…………省略
LoadModule headers_module modules/ mod_headers.so //开启去掉前面#
LoadModule deflate_module modules/ mod_deflate.so
LoadModule filter module modules/mod filter.so
…………省略
#代表对什么样的内容启用gzip压缩
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png
#代表压缩级别
DeflateCompressionLevel 9
#代表启用deflate模块对本站点的输出进行gzip压缩
SetOutputFilter DEFLATE
#语法验证
[root@localhost conf]# /usr/local/httpd/bin/apachectl -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK //验证配置文件成功
#启动服务,关闭防火墙
[root@localhost conf]# /usr/local/httpd/bin/apachectl start
[root@localhost conf]# systemctl stop firewalld.service
[root@localhost conf]# setenforce 0
#网页站点,后期可以在里面添加内容,如图片等
[root@localhost conf]# cd ..
[root@localhost httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@localhost httpd]# cd htdocs
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# cat index.html
It works!
#使用Fiddler工具抓包,进行对比分析Fiddler工具抓包,进行对比分析
目前只有文字,没有进行压缩
我们现在来添加图片,在进行访问并抓包查看
[root@localhost htdocs]# cd /abc
[root@localhost abc]# ls
apr-1.6.2.tar.gz awstats-7.6.tar.gz Discuz_X2.5_SC_UTF8.zip httpd-2.4.29.tar.bz2 john-1.8.0.tar.gz mysql-5.6.26.tar.gz ty.jpg
apr-util-1.6.0.tar.gz cronolog-1.6.2-14.el7.x86_64.rpm extundelete-0.2.4.tar.bz2 httpd-2.4.2.tar.gz LAMP.txt php-5.6.11.tar.bz2
[root@localhost abc]# cp ty.jpg /usr/local/httpd/htdocs/
[root@localhost abc]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
index.html ty.jpg
[root@localhost htdocs]# vim index.html
It works!
通过mod_expire模块配置Apache ,使网页能在客户端浏览器缓存一段时间,以避免重复请求
启用mod_expire模块后,会自动生成页面头部信息中的Expires标签和cache-control标签,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
查看是否安装了mod_expire模块
如果没有安装则要重新编译安装
./configure --enable-expires ……
make && make install
#安装语言包
[root@localhost ~]# yum install -y gcc gcc-c++ pcre pcre-devel zlib-devel expat-devel perl make
#挂载及解压安装包
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.17.1/share /abc
Password for root@//192.168.17.1/share:
[root@localhost abc]# tar zxvf apr-1.6.2.tar.gz -C /opt
[root@localhost abc]# tar zxvf apr-util-1.6.0.tar.gz -C /opt
[root@localhost abc]# tar zxvf httpd-2.4.29.tar.gz -C /opt
[root@localhost abc]# cd /opt
[root@localhost opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.2 rh
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.2/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.2/srclib/apr-util
#执行安装模块并进行编译
[root@localhost opt]# cd httpd-2.4.2
[root@localhost httpd-2.4.2]# ./configure \
--prefix=/usr/local/httpd \
--enable-deflate \
--enable-expires \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
[root@localhost httpd-2.4.2]# make && make install
#复制httpd启动脚本并添加35级别自动运行
[root@localhost httpd-2.4.2]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@localhost httpd-2.4.2]# vim /etc/init.d/httpd
# chkconfig: 35 85 21 //35级别自动运行 第85个启动 第21个关闭
# description: Apache is a World Wide Web server
[root@localhost httpd-2.4.2]# chkconfig --add httpd //将httpd加入到SERVICE管理器
[root@localhost httpd-2.4.2]# chkconfig --level 35 httpd on
#切换至主配置文件进行配置
[root@localhost httpd-2.4.2]# cd /usr/local/httpd/conf
[root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# ln -s /usr/local/httpd/conf/httpd.conf /etc/ //建立软链接便于管理
[root@localhost conf]# ln -s /usr/local/httpd/bin/* /usr/local/bin/ //建立软链接便于管理命令
[root@localhost conf]# vim /etc/httpd.conf
Listen 192.168.17.139:80
#Listen 80
ServerName kgc.example.com:80
LoadModule expires_module modules/mod_expires.so //开启缓存模块 去掉前面#
//配置文件末尾添加启用mod_expires模块并设置
ExpiresActive On
ExpiresDefault "access plus 50 seconds"
[root@localhost conf]# apachectl -t //验证语法
Syntax OK
#关闭防火墙并启用服务
[root@localhost conf]# systemctl stop firewalld
[root@localhost conf]# setenforce 0
[root@localhost conf]# service httpd start
防盗链就是防止别人的网站代码里面盗用服务器的图片、文件、视频等相关资源
如果别人盗用网站的这些静态资源,明显的是会增大服务器的带宽压力
所以作为网站的维护人员,要杜绝我们服务器的静态资源被其他网站盗用
使用两台主机模拟盗链
IP地址 | 域名 | 用途 |
---|---|---|
192.168.17.140 | www.kgc.com | 源主机 |
192.168.17.134 | www.dl.com | 盗链网站 |
检查Apache是否安装了mod_rewrite模块
/usr/local/apache/bin/apachectl -t -D DUMP_MODULES
如果输出中没有rewrite_module (static) ,则说明编译时没有安装mod_rewrite模块
如果没有安装则要重新编译安装
./configure --enable-rewrite…
make && make install
配置规则变量说明
规则匹配说明
RewriteEngine On :打开网页重写功能
RewriteCond :设置匹配规则
RewriteRule :设置跳转动作
规则匹配
修改配置文件启用防盗链功能并设置规则:
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.jpg
#安装语言包
[root@localhost ~]# yum install -y gcc gcc-c++ pcre pcre-devel zlib-devel expat-devel perl make
#挂载及解压安装包
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.17.1/share /abc
Password for root@//192.168.17.1/share:
[root@localhost abc]# tar zxvf apr-1.6.2.tar.gz -C /opt
[root@localhost abc]# tar zxvf apr-util-1.6.0.tar.gz -C /opt
[root@localhost abc]# tar zxvf httpd-2.4.2.tar.gz -C /opt
[root@localhost abc]# cd /opt
[root@localhost opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.2 rh
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.2/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.2/srclib/apr-util
#执行安装模块并进行编译
[root@localhost opt]# cd httpd-2.4.2/
[root@localhost httpd-2.4.2]# ./configure \
--prefix=/usr/local/httpd \
--enable-deflate \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
[root@localhost httpd-2.4.2]# make && make install
#复制httpd启动脚本并添加35级别自动运行
[root@localhost httpd-2.4.2]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@localhost httpd-2.4.2]# vim /etc/init.d/httpd
# chkconfig: 35 85 21 //35级别自动运行 第85个启动 第21个关闭
# description: Apache is a World Wide Web server
[root@localhost httpd-2.4.2]# chkconfig --add httpd //将httpd加入到SERVICE管理器
[root@localhost httpd-2.4.2]# chkconfig --level 35 httpd on
#切换至主配置文件进行配置
[root@localhost httpd-2.4.2]# cd /usr/local/httpd/conf
[root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# ln -s /usr/local/httpd/conf/httpd.conf /etc/ //建立软链接便于管理
[root@localhost conf]# ln -s /usr/local/httpd/bin/* /usr/local/bin/ //建立软链接便于管理命令
[root@localhost conf]# vim /etc/httpd.conf
Listen 192.168.17.139:80
#Listen 80
ServerName kgc.example.com:80
[root@localhost conf]# apachectl -t //验证语法
Syntax OK
#关闭防火墙并启用服务
[root@localhost conf]# systemctl stop firewalld
[root@localhost conf]# setenforce 0
[root@localhost conf]# service httpd start
#安装bind,启用dns
[root@localhost ~]# yum install -y bind
[root@localhost ~]#vim /etc/named.conf
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
};
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.17.140
[root@localhost named]# systemctl start named
#盗链端配置
安装软件
[root@localhost ~]# yum install -y httpd
[root@localhost ~]# echo "nameserver 192.168.17.140" > /etc/resolv.conf //添加DNS地址
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.html //建立网站站点并引用源主机图片url
It test!accp
[root@localhost html]# vim /etc/httpd/conf/httpd.conf //配置主配置文件
Listen 192.168.17.134:80
ServerName www.accp.com:80
#关闭防火墙闭并开启服务
[root@localhost html]# systemctl stop firewalld.service
[root@localhost html]# setenforce 0
[root@localhost html]# systemctl start httpd
#这时其他主机可以自由盗取调用网站图片等静态资源
#再次到主机源主配置文件进行配置防盗链
[root@localhost ~]# vim /etc/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so //开启rewrite功能
//启用防盗链功能并设置规则
Options Indexes FollowSymLinks
…………
AllowOverride None
…………
Require all grabted
…………
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://kgc.com/.*$ [NC]
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.jpg
[root@localhost ~]# service httpd restart //重启服务
#再次测试,盗链端再次查看
配置Apache隐藏版本信息
将主配置文件httpd.conf以下行注释去掉。
#Include conf/extra/httpd-default.conf
修改httpd-default.conf文件两个地方
ServerTokens Full修改为Server Tokens Prod
将ServersSignature On修改为ServersSignature Off
#目前未进行隐藏版本配置,先进行抓包测试
[root@localhost htdocs]# vim /etc/httpd.conf
Include conf/extra/httpd-default.conf //开启 去掉前面的#号
#进入子配置文件
[root@localhost htdocs]# cd /usr/local/httpd/
[root@localhost httpd]# ls
bin build cgi-bin conf error htdocs icons include lib logs man manual modules
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# cd extra/
[root@localhost extra]# ls
httpd-autoindex.conf httpd-languages.conf httpd-ssl.conf
httpd-dav.conf httpd-manual.conf httpd-userdir.conf
httpd-default.conf httpd-mpm.conf httpd-vhosts.conf
httpd-info.conf httpd-multilang-errordoc.conf proxy-html.conf
[root@localhost extra]# vim httpd-default.conf //进入子配置文件
ServerTokens Prod //FULL 改为 Prod 只显示名称不显示版本号
ServerSignature Off //设为OFF
[root@localhost extra]# service httpd restart //重启服务
#隐藏版本配置完成,再次进行抓包测试
现在版本号就已经隐藏了,只显示apache,而一般情况下,每个版本都会有一些隐藏的bug,而隐藏版本号也就从另一方面,间接性的起到了安全防空的作用