文章目录
Apache网页优化
1 网页压缩
1.1 检查是否安装mod_deflate模块
apachectl -t -D DUMP_MODULES | grep "deflate"
如果没有安装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 && make install
1.2 配置mod_deflate模块启用
vim /usr/local/httpd/conf/httpd.conf
--52行--修改
Listen 192.198.80.10:80
--105行--取消注释
LoadModule deflate_module modules/mod_deflate.so
#开启mod_deflate 模块
--197行--取消注释,修改
ServerName www.kgc.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>
1.3 检查安装情况,启动服务
apachectl -t
#验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "deflate"
#检查 mod_deflate 模块是否已安装
deflate_module (shared)
#已安装的正确结果
systemctl start httpd.service
1.4 测试mod_deflate压缩是否生效
cd /usr/local/httpd/htdocs
先将game.jpg文件传到/usr/local/httpd/htdocs目录下
vim index.html
<html>
<body>
<h1>Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!</h1>
<img src="game.jpg">
</body>
</html>
1.5 测试访问
2 网页缓存
2.1 检查是否安装mod_expires模块
apachectl -t -D DUMP_MODULES | grep "expires"
如果没有安装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 -j2 && makeinstall
2.2 配置mod_expires模块启用
vim /usr/local/httpd/conf/httpd.conf
--52行--修改
Listen 192.198.80.10:80
--111行--取消注释
LoadModule expires_module modules/mod_expires.so #开启mod_expires 模块
--199行--取消注释,修改
ServerName www.kgc.com:80
--末行添加--
<IfModule mod_expires.c>
ExpiresActive On
#打开网页缓存功能
ExpiresDefault "access plus 60 seconds"
#设置缓存60秒
</IfModule>
2.3 检查安装情况,启动服务
apachectl -t
#验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "expires"
#检查 mod_deflate 模块是否已安装
expires_module (shared)
#已安装的正确结果
systemctl start httpd.service
3 隐藏版本信息
vim /usr/local/httpd/conf/httpd.conf
--491行--取消注释
Include conf/extra/httpd-default.conf
vim /usr/local/httpd/conf/extra/httpd-default.conf
--55行--修改
ServerTokens Prod
#将原本的 Full 改为 Prod,只显示名称,没有版本
#ServerTokens 表示 Server 回送给客户端的响应头域是否包含关于服务器 OS 类型和编译过的模块描述信息。
systemctl restart httpd.service
4 Apache防盗链
4.1 检查是否安装mod_rewrite模块
apachectl -t -D DUMP_MODULES | grep "rewrite"
如果没有安装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
4.2 配置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://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.png #设置跳转动作
</Directory>
4.3 网页准备
cd /usr/local/httpd/htdocs
将w.jpg、error.png文件传到/usr/local/httpd/htdocs目录下
vim index.html
<html>
<body>
<h1>this is kgc.com!</h1>
<img src="w.jpg">
</body>
</html>
4.4 盗链网站主机
cd /usr/local/httpd/htdocs
vim index.html
<html>
<body>
<h1>this is benet.com!</h1>
<img src="http://www.kgc.com/game.jpg"/>
</body>
</html>
访问盗链网站