Apache 网页与安全优化
在企业中,部署Apache后只采用默认的配置参数,会引发网站很多问题,换言之默认配置是针对以前较低的服务器配置的,以前的配置已经不适用当今互联网时代。为了适应企业需求,就需要考虑如何提升Apache的性能与稳定性,这就是Apache优化的内容。
网页压缩
1.检查是否安装 mod_deflate 模块
apachectl -t -D DUMP_MODULES | grep "deflate"
2.如果没有安装mod_deflate 模块,重新编译安装 Apache 添加 mod_deflate 模块
systemctl stop httpd.service
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak
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-deflate 加入mod_deflate 模块
make -j 2 && make install
3.配置 mod_deflate 模块启用
vim /usr/local/httpd/conf/httpd.conf
--52行--修改
Listen 192.198.100.100:80
--105行--取消注释
LoadModule deflate_module modules/mod_deflate.so 开启mod_deflate 模块
--197行--取消注释,修改
ServerName www.apple.com:80
--末行添加--
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png 代表对什么样的内容启用gzip压缩
DeflateCompressionLevel 9 代表压缩级别,范围为1~9
SetOutputFilter DEFLATE 代表启用deflate 模块对本站点的输出进行gzip压缩
</IfModule>
4.检查安装情况,启动服务
apachectl -t 验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "deflate" 检查 mod_deflate 模块是否已安装
deflate_module (shared) 已安装的正确结果
systemctl start httpd.service
5.测试 mod_deflate 压缩是否生效
cd /usr/local/httpd/htdocs
先将game.jpg文件传到/usr/local/httpd/htdocs目录下
vim index.html
<html><body><h1>You are very good at fighting, what is the use of fighting, and you must have a background and power when you come out of the mix.</h1>
<img src="tupian.jpg"/>
</body></html>
网页缓存
1.检查是否安装 mod_expires 模块
apachectl -t -D DUMP_MODULES | grep "expires"
2.如果没有安装mod_expires 模块,重新编译安装 Apache 添加 mod_expires模块
systemctl stop httpd.service
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak1
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-deflate \
--enable-expires 加入mod_expires 模块
make && make install
3.配置 mod_expires 模块启用
vim /usr/local/httpd/conf/httpd.conf
--52行--修改
Listen 192.198.80.142:80
--111行--取消注释
LoadModule expires_module modules/mod_expires.so 开启mod_expires 模块
--199行--取消注释,修改
ServerName www.apple.com:80
--末行添加--
<IfModule mod_expires.c>
ExpiresActive On 打开网页缓存功能
ExpiresDefault "access plus 60 seconds" 设置缓存60秒
</IfModule>
4.检查安装情况,启动服务
apachectl -t 验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "expires" 检查 mod_deflate 模块是否已安装
deflate_module (shared) 已安装的正确结果
systemctl start httpd.service
5.测试缓存是否生效
cat /usr/local/httpd/htdocs/index.html
Apache 防盗链
防盗链是防止别人的网站代码里面盗用我们自己服务器上面的图片、文件、视频等相关资源;如果别人盗用网站的这些静态资源,明显是会增大服务器的带宽压力;作为网站的维护人员,要杜绝服务器的静态资源被其他网站盗用。
1.检查是否安装 mod_rewrite 模块
apachectl -t -D DUMP_MODULES | grep "rewrite"
2.如果没有安装mod_rewrite 模块,重新编译安装 Apache 添加 mod_rewrite模块
systemctl stop httpd.service
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak2
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 \ #加入mod_rewrite 模块
--enable-charset-lite \
--enable-cgi \
--enable-deflate \
--enable-expires
make && make install
3.配置 mod_rewrite 模块启用
vim /usr/local/httpd/conf/httpd.conf
--157行--取消注释
LoadModule rewrite_module modules/mod_rewrite.so
--224行--
<Directory "/usr/local/httpd/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
RewriteEngine On #打开 rewrite 功能,加入 mode_rewrite 模块内容
RewriteCond %{HTTP_REFERER} !^http://apple.com/.*$ [NC] #设置匹配规则
RewriteCond %{HTTP_REFERER} !^http://apple.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.apple.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.apple.com/$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.apple.com/error.png #设置跳转动作
</Directory>
4.网页准备
cd /usr/local/httpd/htdocs
将game.jpg、error.png文件传到/usr/local/httpd/htdocs目录下
vim index.html
<html><body><h1>You are very good at fighting, what is the use of fighting, and you must have a background and power when you come out of the mix!</h1>
<img src="tupian.jpg"/>
</body></html>
echo "192.168.142.10 www.apple.com" >> /etc/hosts
echo "192.168.142.20 www.banana.com" >> /etc/hosts
盗链网站主机:
cd /usr/local/httpd/htdocs yum安装的httpd服务的默认路径为/var/www/html/
vim index.html
<html><body><h1>this is benet.com!</h1>
<img src="">
</body></html>
echo "192.168.142.10 www.apple.com" >> /etc/hosts
echo "192.168.142.20 www.banana.com" >> /etc/hosts