网页压缩
1.检查是否安装 mod_deflate 模块
apachectl -t -D DUMP_MODULES | grep "deflate"
2.如果没有安装mod_deflate 模块,重新编译安装 Apache 添加
systemctl stop httpd.service #关闭服务
cd /usr/local/httpd/conf #cd到主配置文件目录
mv httpd.conf httpd.conf.bak #把原来的主配置文件备份便于重新编译安装apache添加模块
cd /opt/apache/httpd-2.4.29/ #在软件目录下
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate #加入mod_deflate模块
make -j2 && make install #编译安装
3.配置 mod_deflate 模块启用
vim /usr/local/httpd/conf/httpd.conf
--52行--修改
Listen 192.198.47.20: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>
4.检查安装情况,启动服务
[root@cxx conf]# apachectl -t -D DUMP_MODULES | grep "deflate"
#检查 mod_deflate 模块是否已安装
deflate_module (shared) #已安装的正确结果
[root@cxx conf]# apachectl -t #验证配置文件的配置是否正确
Syntax OK
systemctl start httpd.servic
5.测试 mod_deflate 压缩是否生效
[root@cxx conf]# cd /usr/local/httpd/htdocs/
[root@cxx htdocs]# ls
bbs index.php test
[root@cxx htdocs]# cd test/
[root@cxx test]# ls
error.png game.jpg hello.html
[root@cxx test]# cat hello.html
<html>
<body>
<h1>nihao everyonhao everyone!nihao everyone!nihao everyone!nihao everyone!nihao everyone!e!</h1>
<img src="game.jpg" />
</body>
</html>
方法: 在Linux系统中,打开火狐浏览器,右击点查看元素
选择 网络 ---> 选择 HTML、WS、其他
访问 http://192.168.47.20 ,双击200响应消息查看响应头中包含 Content-Encoding: gzip
网页缓存
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 #cd到主配置文件目录
mv httpd.conf httpd.conf.bak #把原来的主配置文件备份便于重新编译安装apache添加模块
cd /opt/apache/httpd-2.4.29/ #在软件目录下
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate \
--enable-expires #加入mod_deflate模块
make -j2 && make install #编译安装
3.配置 mod_expires 模块启用
vim /usr/local/httpd/conf/httpd.conf
--52行--修改
Listen 192.198.47.20: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>
4.检查安装情况,启动服务
apachectl -t #验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "expires" #检查 mod_deflate 模块是否已安装
deflate_module (shared) #已安装的正确结果
systemctl start httpd.service
5.测试缓存是否生效
[root@cxx htdocs]# cat /usr/local/httpd/htdocs/test/hello.html
<html>
<body>
<h1>nihao everyonhao everyone!nihao everyone!nihao everyone!nihao everyone!nihao everyone!e!</h1>
<img src="game.jpg" />
</body>
</html>
隐藏版本信息
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
Apache 防盗链
1.检查是否安装 mod_rewrite 模块
[root@cxx conf]# apachectl -t -D DUMP_MODULES | grep "rewrite"
rewrite_module (shared)
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 -j2 && 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://kgc.com/.*$ [NC] #设置匹配规则
254 RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/.*$ [NC]
255
256 RewriteRule .*\.(gif|jpg|swf)$ http://www.kgc.com/error.png #设置跳转动作