nginx 防盗链简单配置

在这里插入图片描述
1、准备环境
准备两台机器,一张图片

图片网站服务器:上传图片 192.168.122.94
创建一个存放图片的目录,把图片放到该目录下,并保证该目录能被访问

[root@localhost ]# mkdir /data/imgs -p
[root@localhost ]# cd /data/imgs/
[root@localhost imgs]# ls
txt.png
[root@localhost imgs]# vim /etc/nginx/nginx.conf
server {
      listen       80;
      server_name  localhost;
      location / {
            root   /data/imgs; #修改一下发布目录
        }
}
[root@localhost imgs]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost imgs]# nginx -s reload
用本机测试访问看能不能成功
[root@localhost imgs]# curl 192.168.122.94/txt.png -I

盗链机器配置:192.168.122.169

可以先查看一下配置文件,看看网站发布目录在哪(注:yum安装和编译安装目录不一样)
[root@localhost nginx]# vim /etc/nginx/nginx.conf
[root@localhost nginx]# cd /home/www/nginx
[root@localhost nginx]# vim index.html
<!DOCTYPE html>
<html>
<head>
    <title>PHP 动态网站</title>
</head>
<body>
<img src="http://192.168.122.94/txt.png"/>
</body>
</html>

测试;(如果修改了配置文件还需要重新加载配置文件)
[root@localhost nginx]# curl 192.168.122.169 -I

防盗链机器配置:在图片服务器操作 192.168.122.94

  • none : 允许没有http_refer的请求访问资源;

  • blocked : 允许不是http://开头的,不带协议的请求访问资源—被防火墙过滤掉的;

  • server_names : 只允许指定ip/域名来的请求访问资源(白名单);

[root@localhost html]# vim /etc/nginx/nginx.conf
server {
    listen       80;
    server_name  localhost;
    location / {
         root   /data/imgs;

         valid_referers  nonw blocked www.jd.com;  #允许这些访问
                if ($invalid_referer) {
                      return 403;
                }
         }
}

[root@localhost html]# nginx -s reload

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值