博客
关于我
011.Nginx防盗链
阅读量:439 次
发布时间:2019-03-06

本文共 3667 字,大约阅读时间需要 12 分钟。

一 盗链

1.1 盗链概述

盗链指的是在自己的界面展示非本服务器上的内容,通过技术手段获得其他服务器的资源。绕过他人资源展示页面,在自己页面向用户提供此内容,从而减轻自己服务器的负担,因为真实的空间和流量来自其他服务器。
因此,通常为了避免被盗链,通常Web服务器建议配置防盗链,其主要防盗链思路是能区别哪些请求是非正常用户请求。

二 防盗链

2.1 防盗链配置

语法:valid_referers none | blocked | server_names | string ...;
默认值:——
可配置段:server, location

2.2 环境准备

主机
域名
IP
备注
nginx01
good.linuxds.com
172.24.10.21
被盗方
nginx02
steal.uclouda.com
172.24.10.22
盗链方
添加解析:/etc/hosts
1 172.24.10.21	good.odocker.com  2 172.24.10.22    steal.uclouda.com  3 [root@nginx0X ~]# nginx -V  4 nginx version: nginx/1.16.1
 

2.3 模拟盗链

1 [root@nginx01 ~]# vi /etc/nginx/conf.d/good.conf	#创建模拟被盗方配置  2 server {  3     listen  80;  4     charset utf-8;  5         server_name  good.linuxds.com;  6     location / {  7         root   /usr/share/nginx/good;  8         index  index.html;  9         access_log  /var/log/nginx/good.access.log  main; 10         error_log   /var/log/nginx/good.error.log  warn; 11   } 12 }
 
1 [root@nginx01 ~]# mkdir -p /usr/share/nginx/good/images  2 [root@nginx01 ~]# echo '

Good

' > /usr/share/nginx/good/index.html 3 [root@nginx01 ~]# ll /usr/share/nginx/good/images #上传一张测试图片 4 total 60K 5 -rw-r--r-- 1 root root 4.8K Mar 11 16:27 baidu.png
 
 
1 [root@nginx02 ~]# vi /etc/nginx/conf.d/steal.conf	#创建盗链方配置  2 server {  3     listen  80;  4     charset utf-8;  5     server_name  steal.uclouda.com;  6     location / {  7         root   /usr/share/nginx/steal;  8         index  index.html;  9         access_log  /var/log/nginx/steal.access.log  main; 10         error_log   /var/log/nginx/steal.error.log  warn; 11   } 12 }
 
1 [root@nginx02 ~]# mkdir -p /usr/share/nginx/steal  2 [root@nginx02 ~]# vi /usr/share/nginx/steal/index.html  3   4   5 
盗链图片
6 7 8
 
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件  2 [root@nginx01 ~]# nginx -s reload			#重载配置文件  3 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件  4 [root@nginx02 ~]# nginx -s reload			#重载配置文件
 
浏览器访问:http://good.linuxds.com/images/baidu.png
浏览器访问盗链网站:http://steal.uclouda.com/

2.4 防盗链配置01

1 [root@nginx01 ~]# vi /etc/nginx/conf.d/good.conf  2 server {  3     listen  80;  4     charset utf-8;  5     server_name  good.linuxds.com;  6     location / {  7         root   /usr/share/nginx/good;  8         index  index.html;  9         access_log  /var/log/nginx/good.access.log  main; 10         error_log   /var/log/nginx/good.error.log  warn; 11         valid_referers none blocked good.linuxds.com; 12         if ($invalid_referer) { 13             return 403; 14         } 15   } 16 }
 
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件  2 [root@nginx01 ~]# nginx -s reload			#重载配置文件
 
配置释义:
valid_referers:此关键字定义了白名单,即本机自身访问允许;
invalid_referer:此为内置变量,通过判断上一行中的valid_referers值会返回0或者1,
  • none代表请求头中没有referer信息,这一般是直接在浏览器输入图片网址;
  • blocked代表被防火墙过滤标记过的请求。如果访问来源不在白名单内,则返回403错误
浏览器访问:http://good.linuxds.com/images/baidu.png
浏览器访问盗链网站:http://steal.uclouda.com/
如上所示:已成功配置防盗链。

2.5 防盗链配置02

1 [root@nginx01 ~]# vi /etc/nginx/conf.d/good.conf  2 server {  3     listen  80;  4     charset utf-8;  5         server_name  good.linuxds.com;  6     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {	#针对特定文件类型  7       valid_referers none blocked *.linuxds.com linuxds.com;  8       access_log  /var/log/nginx/good.access.log  main;  9       error_log   /var/log/nginx/good.error.log  warn; 10       if ($invalid_referer) { 11           rewrite ^/ https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1767274412,1868768041&fm=26&gp=0.jpg; 12     } 13   } 14 }
 
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf	#检查配置文件  2 [root@nginx01 ~]# nginx -s reload			#重载配置文件
 
配置释义:
rewrite:判断如果不是白名单第五行则进行重定向到自定义的固定链接。
浏览器访问盗链网站:http://steal.uclouda.com/

转载地址:http://lrwyz.baihongyu.com/

你可能感兴趣的文章
MySQL参数调优详解
查看>>
mysql参考触发条件_MySQL 5.0-触发器(参考)_mysql
查看>>
MySQL及navicat for mysql中文乱码
查看>>
MySqL双机热备份(二)--MysqL主-主复制实现
查看>>
MySql各种查询
查看>>
mysql同主机下 复制一个数据库所有文件到另一个数据库
查看>>
mysql启动以后会自动关闭_驾照虽然是C1,一直是开自动挡的车,会不会以后就不会开手动了?...
查看>>
mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)
查看>>
Mysql启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>