通过Nginx实现图片防盗链

  • A+
所属分类:运维教程

原创性质的图文站,经常会遇到这样的问题:盗链。现在的采集软件特别多,可以通过制定一系列的规则,将别人网站上的资源下载到本地或同步到自己的网站上。

文字型的复制十分简单,图片型的需要额外存储,所以很多采集站长会选择盗链的形式。何为盗链?简单来说,就是将你网站上图片的链接放到别人的网站上,这样在访问那个人的网站时,实际上在调用你网站的图片,不仅使用你的图片,还用你的流量带宽。

这种事情叔可忍婶都忍不了,今天来教大家如何通过Nginx实现图片防盗链。

首先在nginx的配置文件中找到如下内容:

  1. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  2.     {
  3.         expires      30d;
  4.         access_log off;
  5.     }

第一行是文件类型,访问这些文件时按照下面的设置。

第三行是缓存过期时间,就是别人在访问你的网站时,会将这些文件下载到本地缓存中,再次访问时直接读缓存中的内容,加快网站打开速度。这些缓存资源保存30天,超过30天会删除本地的缓存,然后重新下载。

第四行为是否记录访问日志,off不记录。当别人访问你的静态资源时,不做记录,减少访问日志生成。

我们需要将上面的内容改为:

  1. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
  2.         valid_referers none blocked *.birdteam.net server_names ~\.google\. ~\.baidu\.;
  3.         if ($invalid_referer) {
  4.                 rewrite ^/ http://birdteam.net/403.jpg;
  5.         }
  6.         expires      30d;
  7.         access_log off;
  8. }

第二行为判断访问网址,是否通过这个地址访问图片,同时包含谷歌、百度形式访问。红色字体处需替换;

第三行调用第二行的值,如果不是的话,执行第四行的操作;

第四行为返回图片,当遇到盗链时,将此图片返回。红色字体处需替换,同时需要准备以绿色字体命名的图片一张,放在网站根目录中。

  • 问答中心
  • 微信小程序扫一扫
  • weinxin
  • 微信公众
  • 微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:29   其中:访客  29   博主  0

    • avatar grolyrto lemcs

      Keep working ,fantastic job!

      • avatar cbd oil that works 2020

        Hi there to every one, it’s truly a pleasant for me to pay a
        visit this web page, it includes priceless Information.

        • avatar βλεφαρίδες extension

          As I web-site possessor I believe the content material here is rattling fantastic , appreciate it for your efforts. You should keep it up forever! Best of luck.

          • avatar find this

            Enjoyed studying this, very good stuff, thankyou.

            • avatar cbd oil that works 2020

              Have you ever considered about adding a little bit more
              than just your articles? I mean, what you say is fundamental and everything.
              However just imagine if you added some great photos or video clips to
              give your posts more, “pop”! Your content is excellent but with pics and
              videos, this website could undeniably be one of the most beneficial in its niche.
              Terrific blog!

              • avatar cbd oil that works 2020

                I know this if off topic but I’m looking into starting my own weblog and
                was wondering what all is needed to get setup? I’m assuming having
                a blog like yours would cost a pretty penny? I’m not very
                web smart so I’m not 100% sure. Any suggestions or advice
                would be greatly appreciated. Appreciate it

                • avatar tinyurl.com

                  I pay a visit everyday a few blogs and information sites
                  to read articles or reviews, except this webpage gives feature based content.

                  • avatar tinyurl.com

                    I visit every day some web pages and websites to read content, but this weblog presents quality based articles.

                    • avatar kea.obr14.ru

                      I’m not that much of a internet reader to be honest but your sites really nice, keep it up!
                      I’ll go ahead and bookmark your site to come back later.
                      Cheers