nginx设置图片防盗链

编辑Nginx配置  可以用域名也可以用ip

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        valid_referers none blocked b2b_ecae.cn;
        if ($invalid_referer)
        {
            return 403;
        }
        expires      30d;
    }

valid_referers none blocked 后面的域名可以是多个  ip和域名是有区别的  如果域名下外链的图片 要填写域名

重启Nginx即可

测试

[root@localhost ~]# curl -I http://b2b_ecae.cn/test.jpg
HTTP/1.1 200 OK
Server: nginx/1.9.9
Date: Wed, 28 Sep 2022 08:19:13 GMT
Content-Type: image/jpeg
Content-Length: 244222
Last-Modified: Thu, 22 Jul 2021 06:19:29 GMT
Connection: keep-alive
ETag: "60f90df1-3b9fe"
Expires: Fri, 28 Oct 2022 08:19:13 GMT
Cache-Control: max-age=2592000
Accept-Ranges: bytes

[root@localhost ~]# curl -e 'http://www.baidu.com' -I http://b2b_ecae.cn/test.jpg
HTTP/1.1 403 Forbidden
Server: nginx/1.9.9
Date: Wed, 28 Sep 2022 08:19:33 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 168
Connection: keep-alive

或者下面这样测试  html文件放在另外一个非b2b_ecae.cn的域名下




    
    
    nginx设置图片防盗链test


    

你可能感兴趣的:(linux,nginx,linux,nginx)