Nginx防盗链配置指南

防盗链主要用在资源站或文件服务器上,不想让其它网站引用你的资源,避免服务器流量浪费
以下配置主要针对nginx,如果使用COS,OBS等,直接在相应服务商配置即可

防盗链配置

location / {

    # 防盗链 白名单
    valid_referers none blocked server_names *.lovelu.top 127.0.0.1 servicewechat.com ~\.google\. ~\.baidu\. ~\.qq\.;
    set $flag 1;
    if ($invalid_referer) {
        set $flag 0;
    }
    # 正则表达式匹配
    if ($request_uri ~* "(/logo.png)|(/image.jpg)") {
      set $flag 1;
    }
    if ($flag = 0) {
        rewrite ^/ https://ip/upload/2022/07/fangdaolian.png;
    }
    proxy_pass http://127.0.0.1:8090;
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

注意:如果是微信小程序,需要额外将小程序的域名servicewechat.com添加到白名单中
否则小程序无法访问图片

  • 支持排除源站地址
  • 支持排除常见搜索引擎
  • 支持排除指定资源链接
- - - - - 本页内容已结束,喜欢请分享 - - - - -

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容