李思琼php,nginx防盗链

什么是防盗链

防盗链简而言之就是防止第三方或者未进允许的域名访问自己的静态资源的一种限制技术。比如A网站有许多自己独立的图片素材不想让其它网站通过直接调用图片路径的方式访问图片,于是采用防盗链方式来防止。

nginx防盗链

防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息,如果别人只链接了自己网站的图片或某个单独的资源,而不是打开整个页面,这就是盗链,referer就是之前的那个网站域名,正常的referer信息有以下几种

nginx防盗链的代码定义定义合规的引用valid_referers none | blocked | server_names | string ...;拒绝不合规的引用:if ($invalid_referer) {

rewrite ^/.*$ http://www.b.org/403.html

}

参数说明:none:请求报文没有referer首部,比如用户直接在浏览器输入域名访问往web网站,就是没有referer信息

blocked:请求报文由referer信息,但无又有效值为空

server_names:referer首部中包含本主机及nginx监听的server_name

invalid_referer:不合规的feferer引用

实例演示

测试页面index.html

演示nginx防盗链

timg.jpeg

正常配置nginx不做防盗链处理server {

listen 80;

server_name dev.api.dd.com;

root /Users/lidong/Desktop/wwwroot/dd_api/public;

index index.php index.html index.htm;

access_log /Users/lidong/wwwlogs/dev.api.dd.com_access.log;

error_log /Users/lidong/wwwlogs/dev.api.dd.com_error.log;

location ~ [^/]\.php(/|$) {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {

}

try_files $uri $uri/ @rewrite;

location @rewrite {

rewrite ^/(.*)$ /index.php?_url=/$1;

}

}`

运行http://localhost/index.html结果

359414

配置限定的资源文件如果被第三方调用直接返回403server {

listen 80;

server_name dev.api.dd.com;

root /Users/lidong/Desktop/wwwroot/dd_api/public;

index index.php index.html index.htm;

access_log /Users/lidong/wwwlogs/dev.api.dd.com_access.log;

error_log /Users/lidong/wwwlogs/dev.api.dd.com_error.log;

location ~ [^/]\.php(/|$) {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {

valid_referers none blocked dev.api.dd.com;

if ($invalid_referer)

{

return 403;

}

}

try_files $uri $uri/ @rewrite;

location @rewrite {

rewrite ^/(.*)$ /index.php?_url=/$1;

}

}

运行http://localhost/index.html结果

配置限定的资源文件如果被第三方调用直接返回一张404的图片server {

listen 80;

server_name dev.api.dd.com;

root /Users/lidong/Desktop/wwwroot/dd_api/public;

index index.php index.html index.htm;

access_log /Users/lidong/wwwlogs/dev.api.dd.com_access.log;

error_log /Users/lidong/wwwlogs/dev.api.dd.com_error.log;

location ~ [^/]\.php(/|$) {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {

valid_referers none blocked dev.api.dd.com;

if ($invalid_referer)

{

rewrite ^/ http://dev.api.dd.com/404.jpeg;

}

}

try_files $uri $uri/ @rewrite;

location @rewrite {

rewrite ^/(.*)$ /index.php?_url=/$1;

}

}

运行http://localhost/index.html结果

调用的图片显示302

用一张源站的404替换显示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值