您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
9-2 web资源防盗链(基于accesskey)
发布时间:2020-10-08 22:58:27编辑:雪饮阅读()
编译accesskey模块到nginx
基于accesskey的防盗链处理需要给nginx编译一个accesskey模块
模块地址:https://github.com/Martchus/nginx-accesskey/archive/master.zip
目前这个最新版支持nginx1.12.0
接下来我们将这个zip包下载下来并解压后如:
[root@localhost src]# unzip master.zip -d master
Archive: master.zip
c7e1c2db543c861afcebd62e3c67d57aa6f3b33a
creating: master/nginx-accesskey-master/
inflating: master/nginx-accesskey-master/config
inflating: master/nginx-accesskey-master/ngx_http_accesskey_module.c
[root@localhost src]# tree master
master
└── nginx-accesskey-master
├── config
└── ngx_http_accesskey_module.c
1 directory, 2 files
那么我们就下载nginx1.12.0并编译安装如:
./configure --prefix=/usr/local/nginx1120 --user=www --group=www --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --add-module=/usr/local/src/master/nginx-accesskey-master
make && make install
配置站点
安装好之后我们配置好php并且建立3个站点,第一个站点www.website1.com配置上accesskey防盗链规则并精简掉nginx的注释后如:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.website1.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #/html 为网站程序目录
include fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
accesskey on;
accesskey_hashmethod md5;
accesskey_arg sign;
accesskey_signature "jason$remote_addr";
expires 30d;
}
}
server {
listen 80;
server_name www.website2.com;
location / {
root html2;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html2;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #/html 为网站程序目录
include fastcgi_params;
}
}
server {
listen 80;
server_name www.website3.com;
location / {
root html3;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html3;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #/html 为网站程序目录
include fastcgi_params;
}
}
}
上面第一红色部分是配置php解析,这个就不必多说了,是我提前搭建好的一个php以fpm运行的,第二蓝色部分是:
开启accesskey功能
并且accesskey的哈希方法是md5(用于签名)
并且accesskey签名接受键名设置为sign
指定accesskey的签名算法设定
最后一项就没有必要啰嗦了。
那么对于另外两个站点则就是默认配置
然后我们对于站点2我们建立1.php我们按照站点1的允许盗链规则进行输出图片到页面
<?php
function myDump($val){
echo "<pre>";
print_r($val);
echo "</pre>";
}
$sign=md5('jason'.$_SERVER['REMOTE_ADDR']);
echo '<img src="http://www.website1.com/7.jpg?sign='.$sign.'">';
然后对于站点3我们就用正常盗链方式直接引用图片地址,建立1.html
<!DOCTYPE html>
<html>
<body>
<img src="http://www.website1.com/7.jpg"/>
</body>
</html>
然后对于站点1我们再给你根目录上传一个7.jpg图片
最后我们重新启动该nginx服务器或者启动
/usr/local/nginx1120/sbin/nginx -s reload
实验及总结
我们会发现直接在站点1自身访问该图片都是无法访问的,是因为没有遵循盗链规则
而对于站点2,由于我们的php脚本中已经依据盗链规则添加了签名,所以图片访问正常
而对于站点3,用于常规的盗链方法则是无法盗链成功的
注意:
Accesskey目前最大兼容nginx1.12.0
关键字词:php,盗链,accesskey,nginx