Nginx中防盗链链接到的图片,被循环调用失败!

Q:Nginx中防盗链链接到的图片,被循环调用失败!

设置两个web虚拟机web1和web2,web1有一个图片img.html,web2的图片img.html中的图片是盗用web1的,现在web1设置防盗链接,成功阻止web2盗用图片,但是在将web2的盗用访问转到主页html下的2.jpg时,产生循环调用2.jpg且访问失败!

    Request URL: http://web2.devops.com/img.html   //img.html显示
    Request Method: GET
    Status Code: 200 OK
    Remote Address: 192.168.226.128:80
    Referrer Policy: no-referrer-when-downgrade

    Request URL: http://web1.devops.com/1.jpg       //1.jpg显示
    Request Method: GET
    Status Code: 302 Moved Temporarily
    Remote Address: 192.168.226.128:80
    Referrer Policy: no-referrer-when-downgrade

    Request URL: http://192.168.226.128/2.jpg       //2.jpg显示
    Request Method: GET
    Status Code: 302 Moved Temporarily
    Remote Address: 192.168.226.128:80
    Referrer Policy: no-referrer-when-downgrade

A:

1.检查图片以及几个img.html位置

    [root@min1 html]# ls
    2.jpg         index.html      ip    web1
    50x.html.bak  index.html.bak  port  web2


    [root@min1 web1]# pwd
    /usr/local/nginx/html/web1
    [root@min1 web1]# ls
    1.jpg  img.html  index.html


    [root@min1 web2]# pwd
    /usr/local/nginx/html/web2
    [root@min1 web2]# ls
    img.html  index.html

没有问题!

2.检查配置文件

     34     server {
     35         listen 80;
     36         server_name web1.devops.com;
     37         root html/web1;
     38 
     39         location ~ \.(jpg|jpeg|png)$ {
     40         valid_referers web1.devops.com;
     41         if ($invalid_referer) {
     42                 #return 404;
     43         rewrite ^/ http://192.168.226.128/2.jpg  break;
     44                         }
     45                 }
     46         }
     47     server {
     48         listen 80;
     49         server_name web2.devops.com;
     50         root html/web2;
     51         }

web1与web2配置正常。

3.若将rewrite注释掉,return 打开,可以成功在Google浏览器成功返回404。所以防盗链功能成功生效。
4. 打开rewrite,使用谷歌浏览器测试网页->(右键)检查->network->刷新网页->查看页面调用方式,发现2.jpg确实被调用,调用语句没有问题。
5. 检查rewrite和2.jpg是否在web1中,若在web1中可能会被循环调用。
6. 检查主页192.168.226.128是否可以直接访问?

    http://192.168.226.128
    显示

    hello,this is the web1  //显示正常!
    访问网址http://192.168.226.128/2.jpg 失败!

怀疑图片链接有问题!

7.经检查测试得知,是rewrite ^/ http://192.168.226.128/2.jpg break; 可知是这一句出了问题!

因为用IP定向,若http中的server没有一个专门指向此IP的,那么会自动将IP匹配到第一个server,所以出现了循环调用的现象!

解决方案 :

①将web1与web2的server虚拟机放到localhost后面去,让系统自动匹配到localhost的server中。

②为访问的IP单独建立一个虚拟机。

你可能感兴趣的:(Linux,QA)