Nginx 配置之图片防盗链

Nginx 防盗链 About 948 words

配置

location ~* ^/uploads/img/.*?\.(jpe?g|png|gif|svg)$ {
    valid_referers none blocked server_names ~($host);
    if ($invalid_referer) {
        return 403;
    }
    expires max;
    root z_blog_openresty/resources/;
}

含义

第一行: jpe?g|png|gif|svg

表示对jpgjpegpnggif后缀的文件进行防盗链判断

第二行: valid_referers

表示对Nginx$host(网站域名)进行判断

第三行: if{}

如果不是指定Referer就返回403错误。

扩展

只限制 referer 不为空

location ~* ^/uploads/img/.*?\.(jpe?g|png|gif|svg)$ {
    if ($http_referer ~ /^$/) {
        return 403;
    }
    expires max;
    root z_blog_openresty/resources/;
}

或者

location /data {
    valid_referers ~.*;
    if ($invalid_referer) {
        return 403;
    }
    root /usr/share/nginx/html/;
}
location ~* ^/uploads/img/.*?\.(jpe?g|png|gif|svg)$ {
        valid_referers ~.*;
        if ($invalid_referer) {
            return 403;
        }
    expires max;
    root z_blog_openresty/resources/;
}

参考

http://nginx.org/en/docs/http/ngx_http_referer_module.html

Views: 2,291 · Posted: 2019-03-20

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh