您当前的位置:首页 > 计算机 > 服务器 > IIS

iis设置防盗链

时间:02-01来源:作者:点击数:

在IIS服务器上设置防盗链,主要通过检测HTTP请求头中的Referer字段来判断资源访问来源,从而阻止非授权网站的直接调用。由于IIS本身不原生支持URL重写规则,通常需要借助第三方组件或内置的URL重写模块来实现。以下是几种主流的配置方法:

方法一:使用IIS URL重写模块(推荐用于IIS 7及以上版本)

这是最现代且官方支持的方式,适用于IIS 7.5、IIS 8及更高版本。

  1. 安装URL重写模块‌:首先,从微软官方下载并安装URL重写模块。
  2. 编辑web.config文件‌:在网站根目录下的web.config文件中,添加以下配置规则。此示例将阻止除yourdomain.comwww.yourdomain.com之外的所有站点访问图片和压缩文件,并返回404错误页面。
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Prevent Hotlinking" stopProcessing="true">
              <match url="^.*\.(gif|jpg|jpeg|png|bmp|rar|zip)$" ignoreCase="true" />
              <conditions>
                <add input="{HTTP_REFERER}" pattern="^http://(www\.)?yourdomain\.com/.*" negate="true" />
              </conditions>
              <action type="Rewrite" url="/404.htm" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    • match url‌:定义需要保护的文件类型扩展名。
    • conditions‌:{HTTP_REFERER}是关键,negate="true"表示“非此来源”。
    • action‌:定义阻止后的动作,可以是Rewrite(重写为指定页面)或Redirect(重定向)。

对方法一的补充:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="防盗链-允许特定域名" stopProcessing="true">
          <match url=".*\.(jpg|jpeg|png|gif|bmp|webp|mp4|mp3|avi|pdf)$" />
          <conditions>
            <!-- 允许直接访问(无Referer) -->
            <add input="{HTTP_REFERER}" pattern="^$" negate="false" />
            
            <!-- 允许自己的域名 -->
            <add input="{HTTP_REFERER}" pattern="^https?://(www\.)?example\.com" negate="true" />
            
            <!-- 允许其他特定域名 -->
            <add input="{HTTP_REFERER}" pattern="^https?://(www\.)?allowed-domain\.com" negate="true" />
            
            <!-- 可以继续添加更多允许的域名 -->
            <add input="{HTTP_REFERER}" pattern="^https?://trusted-site\.org" negate="true" />
          </conditions>
          <action type="Redirect" url="/blocked.html" />
          <!-- 或者返回403:<action type="CustomResponse" statusCode="403" /> -->
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

更灵活的白名单配置

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="HotlinkProtection" stopProcessing="true">
          <match url=".*\.(jpg|jpeg|png|gif|bmp|ico|svg|css|js|mp4|mp3|flv|swf|pdf)$" />
          <conditions logicalGrouping="MatchAny">
            <!-- 条件1:Referer为空(直接访问或浏览器屏蔽) -->
            <add input="{HTTP_REFERER}" pattern="^$" />
            
            <!-- 条件2:白名单域名 -->
            <add input="{HTTP_REFERER}" pattern="^https?://(www\.)?(example\.com|allowed1\.com|allowed2\.net)" />
            
            <!-- 条件3:允许本地访问 -->
            <add input="{HTTP_REFERER}" pattern="^https?://localhost" />
            
            <!-- 条件4:允许IP直接访问 -->
            <add input="{HTTP_REFERER}" pattern="^https?://\d+\.\d+\.\d+\.\d+" />
          </conditions>
          <!-- 满足以上任一条件都不处理(允许访问) -->
          <action type="None" />
        </rule>
        
        <!-- 黑名单规则:不满足白名单的拒绝访问 -->
        <rule name="BlockHotlinking" stopProcessing="true">
          <match url=".*\.(jpg|jpeg|png|gif|bmp|ico|svg|css|js|mp4|mp3)$" />
          <conditions>
            <add input="{HTTP_REFERER}" pattern="^https?://(www\.)?(example\.com|allowed1\.com|allowed2\.net|localhost)" negate="true" />
          </conditions>
          <action type="Redirect" url="/images/blocked.jpg" />
          <!-- 或者:<action type="AbortRequest" /> -->
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

高级配置示例(按文件类型区分)

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- 图片防盗链 -->
        <rule name="ImageHotlink" enabled="true">
          <match url=".*\.(jpg|jpeg|png|gif|webp)$" />
          <conditions>
            <add input="{HTTP_REFERER}" pattern="^$" negate="true" />
            <add input="{HTTP_REFERER}" pattern="^https?://(www\.)?(mydomain\.com|partner\.com)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/placeholder.jpg" />
        </rule>
        
        <!-- 视频防盗链 -->
        <rule name="VideoHotlink" enabled="true">
          <match url=".*\.(mp4|avi|mov|wmv|flv)$" />
          <conditions>
            <add input="{HTTP_REFERER}" pattern="^$" negate="true" />
            <add input="{HTTP_REFERER}" pattern="^https?://(www\.)?(mydomain\.com|video-partner\.com)" negate="true" />
          </conditions>
          <action type="CustomResponse" statusCode="403" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

方法二:使用ISAPI_Rewrite组件(适用于IIS 6及部分IIS 7环境)

对于较旧的IIS版本,或无法使用URL重写模块的环境,可以使用第三方组件ISAPI_Rewrite。

  1. 下载并安装ISAPI_Rewrite‌:从Helicontech官网下载并安装该组件。
  2. 编辑httpd.ini文件‌:在ISAPI_Rewrite的安装目录下找到httpd.ini文件,添加以下规则。此规则允许trusted-site.comtrusted-site2.com白名单访问,其他来源将被重定向到/no.gif
    RewriteCond Host: (.+)
    RewriteCond Referer: (?!http://(?:www\.trusted-site\.com|www\.trusted-site2\.com)).+
    RewriteRule .*\.(?:gif|jpg|png|exe|rar|zip) /no.gif [I,O]
    • RewriteCond Host: (.+)‌:捕获当前请求的主机名。
    • RewriteCond Referer: (?!...)‌:使用负向前瞻,匹配非白名单来源的Referer。
    • RewriteRule‌:匹配文件类型并重定向到指定图片。
    • [I,O]‌:I表示忽略大小写,O表示优化规则。
  3. 重启IIS‌:修改配置后,需要重启IIS服务使规则生效。

方法三:使用云安全服务(如云锁)

对于希望获得更全面、易用防护的用户,可以考虑使用云安全服务,如云锁。这类服务通常提供图形化界面,支持“引用方式”和“会话方式”两种防盗链机制,并能轻松设置白名单、自定义拦截提示页面和查看防护日志,无需直接操作服务器配置文件。‌‌

重要提示‌:

  • 白名单设置‌:无论采用哪种方法,都建议将搜索引擎(如百度、Google)和自身网站的多个域名(如带www和不带www的版本)添加到白名单,以避免影响SEO和正常访问。
  • 测试‌:配置完成后,务必使用不同来源的链接进行测试,确保防盗链规则按预期工作。
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐