一、防盗链优化
1)防盗链就是防止别人的网站代码里面盗用服务器的图片
文件、视频等相关资源
2)如果别人盗用网站的这些静态资源,明显的是会增大服
务器的带宽压力
3)所以作为网站的维护人员,要杜绝我们服务器的静态资
源被其他网站盗用
1、配置规则变量说明
%{HTTP_ REFERER}: 浏览header中的链接字段,存放一一个链
接的URL,代表是从哪个链接访问所需的网页
1)!^:不以后面的字符串开头
2).*S:以任意字符结尾
3)NC:不区分大写
4)R:强制跳转
2、规则匹配说明
1)RewriteEngine On:打开网页重写功能
2)RewriteCond:设置匹配规则
3)RewriteRule:设置跳转动作
3、规则匹配
1)如果相应变量的值匹配所设置的规则,则逐条往下处理;如果不
匹配,则往后的规则不再匹配
下载并编译安装。要注意,在./配置的时候,要添加以下模块
./configure \
--prefix=/usr/local/httpd \ ##安装目录
--enable-deflate \ ##压缩模块
--enable-expires \ ##缓存模块
--enable-so \
--enable-rewrite \ ##启用重写功能
--enable-charset-lite \
--enable-cgi
主配置文件修改
vim /usr/local/httpd/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so ##开启伪静态模块
<Directory "/usr/local/httpd/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://localhost/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://localhost$ [NC]
RewriteCond %{HTTP_REFERER} !^http://localhost/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://localhost/$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://localhost/盗链图片 ##放在网站站点目录下,盗链原图片会重定向到这张图片上
< /Directory >
查看模块是否正常开启
[root@localhost ~]# cd /usr/local/httpd/bin/
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "rewrite"
rewrite_module (shared)
二、隐藏版本信息
打开主配置文件,开启default配置文件
vim /usr/local/httpd/conf/httpd.conf
Include conf/extra/httpd-default.conf ##开启
vim /usr/local/httpd/conf/extra/httpd-default.conf
ServerTokens Prod ##只显示名称,没有版本
ServerSignature Off
shell脚本部署——防盗链和隐藏版本
脚本中解压的压缩包可以网上自行下载或到https://download.csdn.net/download/yplayer001/11899508下载
其中有一些要根据自己电脑的配置文件修改,比如(下面sed从249行开始写入,这要看自己的配置文件,具体怎么做可以留言或自行百度)
sed -i '249i RewriteEngine On \
RewriteCond %{HTTP_REFERER} !^http://benet.com/.*$ [NC] \
RewriteCond %{HTTP_REFERER} !^http://benet.com$ [NC] \
RewriteCond %{HTTP_REFERER} !^http://www.benet.com/.*$ [NC] \
RewriteCond %{HTTP_REFERER} !^http://www.benet.com/$ [NC] \
RewriteRule .*\\.(gif|jpg|swf)$ http://www.benet.com/error.jpg' \
/usr/local/httpd/conf/httpd.conf
脚本如下(不懂可以评论问):
#!/bin/bash
#Apache的防盗链和隐藏版本号
#---------------------配置dns服务--------------------------
yum install bind -y
a=/etc/named.conf
b=/etc/named.rfc1912.zones
sed -i -e '13s/127.0.0.1/any/' -e '21s/localhost/any/' $a
sed -i -e '19s/localhost/kgc.com/' $b
sed -i -e '21s/named.localhost/kgc.com.zone/' $b
cp -p /var/named/named.localhost /var/named/kgc.com.zone
sed -i 10'd' /var/named/kgc.com.zone
echo "www IN A 192.168.154.152" >> /var/named/kgc.com.zone
systemctl stop firewalld.service
setenforce 0
systemctl start named
#-------------------Apache压缩和缓存--------------------
#将文件放在root家目录中直接解压,解压到/opt文件中
tar zvxf apr-1.6.2.tar.gz -C /opt
tar zvxf apr-util-1.6.0.tar.gz -C /opt
tar jvxf httpd-2.4.29.tar.bz2 -C /opt
mv /opt/apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
mv /opt/apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util
#安装软件包
yum -y install \
gcc \
gcc-c++ \
pcre \
pcre-devel \
zlib-devel \
expat-devel
cd /opt/httpd-2.4.29
./configure \
--prefix=/usr/local/httpd \
--enable-expires \
--enable-deflate \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
#编译和安装
make && make install
#配置监听端口
sed -i '51s/#Listen 12.34.56.78:80/Listen 192.168.154.152:80/' /usr/local/httpd/conf/httpd.conf
sed -i '52s/^/#/' /usr/local/httpd/conf/httpd.conf
#配置服务名称
sed -i '198s/#/ /' /usr/local/httpd/conf/httpd.conf
sed -i 's/example/kgc/' /usr/local/httpd/conf/httpd.conf
sed -i '156s/#/ /' /usr/local/httpd/conf/httpd.conf ##开启防盗链
##开启隐藏版本
sed -i '496s/#/ /' /usr/local/httpd/conf/httpd.conf
sed -i '55s/Full/Prod/' /usr/local/httpd/conf/extra/httpd-default.conf
sed -i '249i 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.png' \
/usr/local/httpd/conf/httpd.conf
cd /usr/local/httpd/bin/
./apachectl -t
./apachectl start
推荐一篇Apache防盗链配置博客https://blog.csdn.net/kangshuo2471781030/article/details/79182527