针对不同的生产环境,我们需要作出不同的针对优化,不仅要考虑Apache的稳定,更要考虑其安全性。
1:查看是否安装mod_deflate模块
2:修改配置文件,启用压缩功能
3:抓包测试
执行apachectl -t -D DUMP_MODULES命令
如果输出中没有deflate_module(static),说明编译时没有安装mod_deflate模块
没有安装则需要从新编译安装
./configure --enable-deflate…
make && make install
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
'代表对什么样的内容启用gzip压缩'
DeflateCompressionLevel '代表压缩级别'
SetOutputFilter DEFLATE '代表启用deflate模块对本站点的输出进行gzip压缩'
重启Apache服务,再用浏览器访问测试网站,并用Fiddler工具抓包,进行对比分析
安装三个软件包,apr-1.6.2.tar.gz,apr-util-1.6.0.tar.gz,httpd-2.4.29.tar.bz2
将跨平台组件包移动到源码包目录下
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf apr-1.6.2.tar.gz
[root@localhost opt]# tar zxvf apr-util-1.6.0.tar.gz
[root@localhost opt]# tar jxvf httpd-2.4.29.tar.bz2
[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]# 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-deflate \ '开启deflate压缩模块'
--enable-so \ '开启核心功能模块'
--enable-rewrite \ '开启重写功能,如防盗链保护'
--enable-charset-lite \ '开启字符集'
--enable-cgi '开启通用网关接口'
[root@localhost httpd-2.4.29]# make && make install '编译和安装'
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[root@localhost httpd-2.4.29]# vim /etc/httpd.conf
'搜索/head和/filter(过滤)是否都都开启的
LoadModule headers_module modules/mod_headers.so
LoadModule filter_module modules/mod_filter.so
'搜索/deflate,如果没有,则需要检查之前配置并重新编译安装'
LoadModule deflate_module modules/mod_deflate.so #注释掉
'在Apache主配置文件末尾添加(放一张jpg图片在网页中做测试查看gzip)'
<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>
'修改监听地址'
Listen 20.0.0.51:80
#Listen 80
'取消注释,修改域名'
ServerName www.kevin.com:80
[root@localhost httpd]# /usr/local/httpd/bin/httpd -t ’检测httpd语法是否正确‘
Syntax OK
[root@localhost httpd]# /usr/local/httpd/bin/httpd '开启httpd'
[root@localhost httpd]# netstat -ntap |grep 80
tcp6 0 0 :::80 :::* LISTEN 55758/httpd
[root@localhost httpd]# cd /usr/local/httpd/bin
[root@localhost bin]# ./apachectl -t
Syntax OK ’验证配置文件成功‘
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "deflate" '查看是否安装mod_deflate模块'
deflate_module (shared)
[root@localhost bin]# 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 htdocs/
[root@localhost htdocs]# ls
index.html
拖一张JPG的图片
[root@localhost htdocs]# vim index.html
'添加下面内容'
<img src="chiji.jpg"\>
[root@localhost htdocs]# iptables -F
[root@localhost htdocs]# setenforce 0
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
</IfModule>
重启httpd服务
基于上一个压缩实验
[root@localhost htdocs]# cd /opt/httpd-2.4.29/
[root@localhost httpd-2.4.29]# ./configure \
--prefix=/usr/local/httpd \
--enable-deflate \
--enable-expires \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
[root@localhost httpd-2.4.29]# make && make install
[root@localhost httpd-2.4.29]# vim /etc/httpd.conf
LoadModule expires_module modules/mod_expires.so '#注释掉,开启缓存模块'
shirt+g到最末添加
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 50 seconds"
</IfModule>
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/bin
[root@localhost bin]# ./apachectl -t '检测语法是否有问题'
Syntax OK
[root@localhost bin]# ./apachectl
httpd (pid 55758) already running
[root@localhost etc]# pkill 55758
[root@localhost bin]# ./apachectl start '开启服务'
[root@localhost bin]# netstat -ntap | grep httpd
tcp6 0 0 :::80 :::* LISTEN 55758/httpd
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "expires"
expores_module (shared)
[root@localhost bin]# systemctl stop firewalld.service
[root@localhost bin]# setenforce 0
IP地址 | 域名 | 用途 |
---|---|---|
20.0.0.51 | www.kevin.com | 源主机(官方网站) |
20.0.0.47 | www.test.com | 盗链网站 |
客户端 | windows 10 | 网页测试 |
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://kevin.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.kevin.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.kevin.com/$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.kevin.com/error.png
[root@localhost ~]# hostnamectl set-hostname kevin
[root@localhost ~]# su
[root@kevin ~]# iptables -F
[root@kevin ~]# setenforce 0
[root@kevin ~]# yum install bind -y
[root@kevin ~]# vim /etc/named.conf
options {
listen-on port 53 { any;}; '修改为any'
...省略内容
allow-query { any; }; '修改为any'
[root@localhost bin]# vim /etc/named.rfc1912.zones
'添加以下内容到正向解析中'
zone "kevin.com" IN {
type master;
file "kevin.com.zone";
allow-update { none; };
};
[root@kevin ~]# cd /var/named/
[root@kevin named]# cp -p named.localhost kevin.com.zone
[root@kevin named]# vim kevin.com.zone
www IN A 20.0.0.51
[root@kevin named]# systemctl start named
[root@kevin named]# tar zxvf apr-1.6.2.tar.gz -C /opt
[root@kevin named]# tar zxvf apr-util-1.6.0.tar.gz -C /opt
[root@kevin named]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt/
[root@kevin named]# cd /opt/
[root@kevin opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh
[root@kevin opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@kevin opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@kevin opt]# cd httpd-2.4.29/
[root@kevin httpd-2.4.29]# yum install gcc gcc-c++ pcre pcre-devel perl expat-devel zlib-devel -y
[root@kevin httpd-2.4.29]# ./configure \
--prefix=/usr/local/httpd \
--enable-deflate \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
[root@kevin httpd-2.4.29]# make && make install
[root@kevin httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/
[root@kevin httpd-2.4.29]# cd /usr/local/httpd/htdocs
'添加两张图片,在Shell中是可以直接拖进来的'
[root@kevin htdocs]# vim index.html
<img src="chiji.jpg"\>
[root@kevin bin]# vim /etc/httpd.conf
Listen 20.0.0.51:80
#Listen 80
ServerName www.kevin.com:80
[root@kevin htdocs]# cd /usr/local/httpd/bin/
[root@kevin bin]# ./apachectl start
[root@kevin bin]# netstat -ntap | grep 80
tcp 0 0 20.0.0.51:80 0.0.0.0:* LISTEN 45653/httpd
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install httpd -y
[root@localhost yum.repos.d]# vim /etc/httpd/conf/httpd.conf
Listen 20.0.0.47:80
#Listen 80
ServerName www.test.com:80
[root@localhost yum.repos.d]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.html
’编辑下面内容做测试‘
<h1>this is test web</h1>
<img src="http://www.kevin.com/chiji.jpg"\>
[root@localhost html]# echo "nameserver 20.0.0.51" > /etc/resolv.conf
[root@localhost html]# systemctl start httpd.service
发现盗链网站可以获取官方网站的图片
[root@kevin bin]# vim /etc/httpd.conf
'搜索rewrite,将#注释掉'
LoadModule rewrite_module modules/mod_rewrite.so
'搜索Directory,将以下文字添加到 '
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://kevin.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://kevin.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.kevin.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.kevin.com/$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.kevin.com/error.png
[root@kevin bin]# ./apachectl stop
[root@kevin bin]# ./apachectl start
[root@localhost httpd]# cd conf/extra
[root@localhost extra]# vim httpd-default.conf
'将以下内容修改'
Include conf/extra/httpd-default.conf '开启,取消注释'
ServerTokens Prod '将原本的Full修改为Prod
ServerSignature Off '//保持是Off'
[root@localhost conf]# cd /usr/local/httpd/bin
[root@localhost bin]# ./apachectl stop
[root@localhost bin]# ./apachectl start