首页 > 解决方案 > IIS 删除图像的 nosniff 标头

问题描述

我有一个返回图像的 API,例如:

/api/products/images/233 -> 这将返回一个 233.jpg 图像

但是我的 IIS 有一条规则将 X-Content-Type-Options 标头添加到安全请求中,但这会破坏 Internet Explorer 上的图像,因此当端点 /products/images/ 是时,我需要一种方法来删除此规则仅当它不是那个端点时才调用或添加标头的方法。

我尝试将其用于自定义标题

但它没有用,我试过这样:

<system.webServer>
    <rewrite>
      <outboundRules>
        <rule name="Remove nosniff">
          <match serverVariable="RESPONSE_X_Content_Type_Options" pattern="/products/images/" />
          <action type="Rewrite" value="none"/>
        </rule>
      </outboundRules>
    </rewrite>
  </system.webServer>

但它并没有改变任何东西,图像仍然有“nosniff”标题。

我错过了一些配置吗?还是有另一种方法可以做到这一点?

标签: iis

解决方案


您的匹配条件是检查标头是否RESPONSE_X_Content_Type_Options包含值/products/images/而不是nosniff. 您可以使用 Location 块将此规则限制为/products/images/,然后用于pattern="nosniff"查找值nosniff

<configuration>
    ...
    <system.webServer/>
    ...
    <location path="products/images/">
        <system.webServer>
            <rewrite>
                <outboundRules>
                    <rule name="Remove nosniff">
                        <match serverVariable="RESPONSE_X_Content_Type_Options" pattern="nosniff" />
                        <action type="Rewrite" value="none"/>
                    </rule>
                </outboundRules>
            </rewrite>
        </system.webServer>
    </location>
</configuration>

有关元素,请参阅文档:https ://msdn.microsoft.com/en-us/library/b6x6shw7(v=vs.100).aspx


推荐阅读