CentOS服务器下nginx防盗链介绍与配置

转载来源 : CentOS服务器下nginx防盗链介绍与配置 : safebase.cn/article-256622-1.html

一、防盗链介绍

1.什么是防盗链

简单的说,就是某些不法的网站,通过在其自身网站程序里为经许可非法调用其他网站资源然后在自己的网站上显示这些调用的资源,达到了填充自身网站显示的效果,但是浪费了调用资源网站的网站流量,造成其他网站的带宽及服务压力吃紧,甚至宕机。

二、配置防盗链

1.配置三个个站点

[root@lnmp ~]# cd /application/nginx/
[root@lnmp nginx]# cat conf/nginx.conf      
worker_processes  1;
events {
    worker_connections  1024;
    use epoll;
}
http {
    server_tokens off;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.liang.com;
            root   html/www;
            index  index.php index.html index.htm;
        location ~ \.php$ {
            root           html/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
                    }        
        }

        server {
        listen       80;
        server_name  bbs.liang.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
                }
        }
        server {
        listen       80;
        server_name  blog.liang.com;
        location / {
            root   html/blog;
            index  index.html index.htm;
                }
        }
}

2.测试是否能访问

[root@lnmp nginx]# curl www.liang.com/index.html       
https://blog.csdn.net/liang_operations/
[root@lnmp nginx]# curl bbs.liang.com                 
bbs
[root@lnmp nginx]# curl blog.liang.com
blog

3.模仿环境配置

3.1www.liang.com为被盗

[root@lnmp nginx]# mkdir html/www/ima   
[root@lnmp nginx]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%     277 KB  277 KB/s 00:00:01       0 Errors
[root@lnmp nginx]# mv  timg.jpg html/www/ima/

3.2bbs.liang.com盗取www的链接

[root@lnmp nginx]# cat html/bbs/index.html   
"http://www.liang.com/ima/timg.jpg">

3.3访问测试

4.www配置防盗

[root@lnmp nginx]#vi conf/nginx.conf
    server {
        listen       80;
        server_name  www.liang.com;
            root   html/www;
            index  index.php index.html index.htm;
        location ~ \.php$ {
            root           html/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
                    }
        location ~* \.(gif|jpg|png|swf|flv|bmp)$ {
            valid_referers none blocked www.liang.com;
            if ($invalid_referer) {
                rewrite ^/ http://blog.liang.com/ima/b.jpg;
                  }
           }        
      }
######

代码详解:

第一行: location ~* .(gif|jpg|png|swf|flv|bmp)$
其中“gif|jpg|png|swf|flv|bmp”设置防盗链文件类型,自行修改,每个后缀用“|”符号分开!
第二行: valid_referers none blocked www.liang.com;
就是白名单,允许文件链出的域名白名单,自行修改成您的域名! 可以使用子域名,域名与域名之间使用空格隔开!
第五行:rewrite ^/ http://blog.liang.com/ima/b.jpg;
这个图片是盗链返回的图片,也就是替换盗链网站所有盗链的图片。这个图片要放在没有设置防盗链的网站上,因为防盗链的作用,这个图片如果也放在防盗链网站上就会被当作防盗链显示不出来了,盗链者的网站所盗链图片会显示X符号。
这样设置差不多就可以起到防盗链作用了。

5.配置blog

[root@lnmp nginx]# ll html/blog/ima/
total 12
-rw-r–r--. 1 root root 11988 Aug 9 2018 b.jpg

6.网页测试

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值