防止IIS 10无法正常工作-Windows服务器2016

问题描述 投票:0回答:1

我不知道如何防止在IIS 10上进行热链接-web.config。我在Google上找到了一个解决方案,但似乎无法正常工作,这是我的代码:

            <rule name="Hotlinking Preventing" stopProcessing="true">
                <match url=".*\.(png|jpe?g|gif)" />
                <conditions>
                    <add input="{HTTP_REFERER}" pattern="^$" negate="true" />
                    <add input="{HTTP_REFERER}" pattern="^https?://(m\.)?domain\.com/.*$" negate="true" />
                </conditions>
                <action type="Rewrite" url="/assets/images/hotlinking.png" />
            </rule>

[任何想法:(?非常感谢。

iis web-config hotlinking
1个回答
0
投票

[作为网站开发人员,有时我们不希望直接引用我们自己网站上的图像并将其显示在其他人的网站上。在某些情况下,它可能会为我们的数据中心带来大量的网络带宽,这意味着我们要花钱来支付使用我们图像的用户。

例如,您的网站是www.sample1.com,您在http://www.sample1.com/test.jpg上有一个图片,而www.sample2.com在www.sample2.com上通过在HTML中添加标签使用了您的图片,这可能会引起网络请求进入服务器消耗资源。

enter image description here

如果用户登陆www.sample2.com并访问http://www.sample1.com/test.jpg,对于www.sample1.com的Web服务器,针对该特定图像的HTTP请求将具有一个名为“ referer”的HTTP标头,其值为“http://www.sample1.com....“。这是我们将检查并阻止请求的地方。

URL重写规则:

 <rule name="Prevent Image Hotlinking">
<match url=".*\.(jpg|jpeg|png|gif|bmp)$" />
<conditions>
                    <add input="{HTTP_REFERER}" pattern="^$" negate="true" />
                    <add input="{HTTP_REFERER}" pattern="^http://www.sample1.com/.*$" negate="true" />
</conditions>
<action type="Rewrite" url="/img/no_hotlinking.png" />

enter image description here

enter image description here

如果仍然不起作用,请尝试禁用缓存,然后重试。

如果您要阻止多个站点,则还可以使用重写映射并设置站点列表。

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Blacklist block" stopProcessing="true">
          <match url="(?:jpg|jpeg|png|gif|bmp)$" />
          <conditions>
              <add input="{HTTP_REFERER}" pattern="^https?://(.+?)/.*$" />
              <add input="{DomainsBlackList:{C:1}}" pattern="^block$" />
              <add input="{REQUEST_FILENAME}" pattern="splog.png" negate="true" />
          </conditions>
          <action type="Redirect" url="http://www.hanselman.com/images/splog.png" appendQueryString="false" redirectType="Temporary"/>
      </rule>
    </rules>
    <rewriteMaps>
              <rewriteMap name="DomainsBlackList" defaultValue="allow">
                  <add key="google-chrome-browser.com" value="block" />
                  <add key="www.verybadguy.com" value="block" />
                  <add key="www.superbadguy.com" value="block" />
              </rewriteMap>
    </rewriteMaps>
  </rewrite>
</system.webServer>
© www.soinside.com 2019 - 2024. All rights reserved.