在IIS服务器上设置防盗链,主要通过检测HTTP请求头中的Referer字段来判断资源访问来源,从而阻止非授权网站的直接调用。由于IIS本身不原生支持URL重写规则,通常需要借助第三方组件或内置的URL重写模块来实现。以下是几种主流的配置方法:
这是最现代且官方支持的方式,适用于IIS 7.5、IIS 8及更高版本。
<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>
对方法一的补充:
<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>
对于较旧的IIS版本,或无法使用URL重写模块的环境,可以使用第三方组件ISAPI_Rewrite。
RewriteCond Host: (.+)
RewriteCond Referer: (?!http://(?:www\.trusted-site\.com|www\.trusted-site2\.com)).+
RewriteRule .*\.(?:gif|jpg|png|exe|rar|zip) /no.gif [I,O]
对于希望获得更全面、易用防护的用户,可以考虑使用云安全服务,如云锁。这类服务通常提供图形化界面,支持“引用方式”和“会话方式”两种防盗链机制,并能轻松设置白名单、自定义拦截提示页面和查看防护日志,无需直接操作服务器配置文件。
重要提示:

