如何从 IIS7 中删除 eTag 标头?

发布于 2024-07-11 19:31:46 字数 214 浏览 5 评论 0原文

根据雅虎高性能网站的最佳实践,我想从我的网站中删除 Etag标头(我手动管理所有缓存,不需要 Etag...并且当/如果我需要扩展到农场时,我真的希望它们消失)。 我在 Windows Server 2008 上运行 IIS7。有人知道我该怎么做吗?

Per Yahoo's best practices for high performance web sites, I'd like to remove Etags from my headers (I'm manually managing all my caching and have no need for Etags... and when/if I need to scale to a farm, I'd really like them gone). I'm running IIS7 on Windows Server 2008. Anyone know how I can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

凉月流沐 2024-07-18 19:31:47

我们遇到了这个问题,甚至在 IIS 7 中设置空白的自定义 ETag 标头也不适用于所有文件(例如图像文件)。 我们最终创建了一个显式删除 ETag 标头的 HttpModule。

We had this problem, and even setting a blank custom ETag header in IIS 7 was not working for all files (for example image files). We ended up creating an HttpModule that explicitly removes the ETag header.

醉态萌生 2024-07-18 19:31:47

更新: 感谢用户 @ChrisBarr 添加了 URL 重写模块要求

在 iis 6 中这很简单,您可以在 IIS 7 中为 'ETag' = "" 添加自定义标头

,在阅读此线程并确定它之后如果不使用自定义http模块是不可能的,我发现你可以简单地安装Microsoft的URL重写模块< /a> 并添加出站重写规则,如下所示:

<outboundRules>
  <rule name="Remove ETag">
    <match serverVariable="RESPONSE_ETag" pattern=".+" />
    <action type="Rewrite" value="" />
  </rule>
</outboundRules>

这实际上有效,并且您不需要自定义 http 模块 (dll)。 解锁 system.webServer 配置部分并设置 customHeaders 等不起作用 - 至少在我尝试过的所有情况下都是如此。 一个简单的出站重写规则即可。

UPDATE: added URL Rewrite Module requirement thanks to user @ChrisBarr

In iis 6 it's easy, you can add a custom header for 'ETag' = ""

In IIS 7, after reading this thread and figuring that it was impossible without using a custom http module, I found that you can simply install Microsoft's URL Rewrite module and add an outbound rewrite rule as follows:

<outboundRules>
  <rule name="Remove ETag">
    <match serverVariable="RESPONSE_ETag" pattern=".+" />
    <action type="Rewrite" value="" />
  </rule>
</outboundRules>

This actually works, and you don't need a custom http module (dll). Unlocking the system.webServer configuration section and setting customHeaders, etc., does not work - at least in all the cases I tried. A simple outbound rewrite rule does.

等风来 2024-07-18 19:31:47

顺便说一句,当您使用iis8时,很简单

<element name="clientCache">
   <attribute name="cacheControlMode" type="enum" defaultValue="NoControl">
          <enum name="NoControl" value="0" />
          <enum name="DisableCache" value="1" />
          <enum name="UseMaxAge" value="2" />
          <enum name="UseExpires" value="3" />
  </attribute>
  <attribute name="cacheControlMaxAge" type="timeSpan" defaultValue="1.00:00:00" />
  <attribute name="httpExpires" type="string" />
  <attribute name="cacheControlCustom" type="string" />
  <attribute name="setEtag" type="bool" defaultValue="true" />
</element>

IIS 8.0:使用或不使用 ETag

By the way, when you use iis8 it's simple

<element name="clientCache">
   <attribute name="cacheControlMode" type="enum" defaultValue="NoControl">
          <enum name="NoControl" value="0" />
          <enum name="DisableCache" value="1" />
          <enum name="UseMaxAge" value="2" />
          <enum name="UseExpires" value="3" />
  </attribute>
  <attribute name="cacheControlMaxAge" type="timeSpan" defaultValue="1.00:00:00" />
  <attribute name="httpExpires" type="string" />
  <attribute name="cacheControlCustom" type="string" />
  <attribute name="setEtag" type="bool" defaultValue="true" />
</element>

IIS 8.0: To use or not to use ETag

允世 2024-07-18 19:31:47

http ://www.jesscoburn.com/archives/2008/10/02/quickly-configure-or-disable-etags-in-iis7-or-iis6/ 有一个很好的图片指南。

本质上,您创建一个名为 ETag 的自定义响应标头并将其值设为空。

http://www.jesscoburn.com/archives/2008/10/02/quickly-configure-or-disable-etags-in-iis7-or-iis6/ has a nice pictorial guide.

Essentially, you create a custom response header named ETag and make its value empty.

沩ん囻菔务 2024-07-18 19:31:47

查看这篇博客文章,了解如何完全删除 iis6、iis7 和 iis7.5 中的 Etag http 标头

http://lightspeednow.com/blog/2010/05/21/iis-tutorial -如何从-iis6-iis7-和-iis7-5中完全删除-etags-entity-tags/

Check out this blog post on how to completely remove the Etag http header in iis6,iis7 and iis7.5

http://lightspeednow.com/blog/2010/05/21/iis-tutorial-how-to-completely-remove-etags-entity-tags-from-iis6-iis7-and-iis7-5/

风向决定发型 2024-07-18 19:31:47

我使用了 http://www 上找到的 removeetag.dll .caspianit.co.uk/iis7-etag-problem/ 它运行得很好。

希望它也适合你

I Used the removeetag.dll found on http://www.caspianit.co.uk/iis7-etag-problem/ and it worked perfectly.

hope it will work well for you too

吹梦到西洲 2024-07-18 19:31:47

在 IIS 7 中,您不必再担心 etag,因为 IIS 配置号始终设置为 0。

如果您有 IIS6 和 IIS,那么仍然存在问题。 同一场中的 IIS7 Web 服务器。 在这种情况下,您必须手动将 IIS6 配置号设置为 0,如 文章

Etag 实际上非常有用,因为您不需要像堆栈溢出那样更改文件名(即default.css?1234)。 如果更改 default.css 文件,它将更改 etag,因此后续请求将从服务器获取文件而不是缓存。

In IIS 7 you shouldn't have to worry about etags anymore as the IIS configuration number is always set to 0.

There is still a problem if you have IIS6 & IIS7 webservers in the same farm. In this case you would have to manually set the IIS6 config number to 0 as described in this article.

Etags are actually very useful as you don't need to change the filename like stack overflow does (i.e. default.css?1234). If you change the default.css file it will change the etag and therefore subsequent requests will get the file from the server and not the cache.

多孤肩上扛 2024-07-18 19:31:47

我认为这会起作用..我知道删除和空白不起作用。

    <configuration>
     <system.webServer>
       <httpProtocol>
          <customHeaders>
            <add name="ETag" value=" " /> 
          </customHeaders>
        </httpProtocol>
       </configuration>
     </system.webServer>

I think this will work .. I know remove and blank doesn't work.

    <configuration>
     <system.webServer>
       <httpProtocol>
          <customHeaders>
            <add name="ETag" value=" " /> 
          </customHeaders>
        </httpProtocol>
       </configuration>
     </system.webServer>
ヅ她的身影、若隐若现 2024-07-18 19:31:46

在 IIS7 下,Etag 更改编号(Etag 中以下部分:)始终设置为 0。

因此,同一文件的服务器的 Etag 不再因服务器而异,因此 Yahoo 最佳实践不再真正适用。

由于您实际上无法在 IIS7 上抑制 ETag 标头,因此最好不要摆弄它。 到目前为止,我发现最有用的配置规则是“如果默认设置不会破坏某些内容,则不要管它”。

Under IIS7 the Etag change number (the part of the Etag following : ) is always set to 0.

Hence the Etag from the server no longer varies from server to server for the same file and therefore the Yahoo best practice no longer really applies.

Since you can't actually suppress the ETag header on IIS7 it would probably be best that you don't fiddle with it at all. I've found by far the most useful configuration rule is "If the default doesn't break something, leave it alone".

对你再特殊 2024-07-18 19:31:46

您可能认为在 web.config 中执行此操作可以禁用 IIS7 中的 ETag。 但嗅探器跟踪确认 ETag 无论如何都已发送。

<httpProtocol>
    <customHeaders>
        <remove name="ETag" />
    </customHeaders>
</httpProtocol>

使用空白也不起作用。 ETag 无论如何都会被发送下来。

<httpProtocol>
    <customHeaders>
        <add name="ETag" value="" />
    </customHeaders>
</httpProtocol>

按照其他网站的建议将 ETag 设置为空白引号是行不通的。

<httpProtocol>
    <customHeaders>
        <add name="ETag" value="""" />
    </customHeaders>
</httpProtocol>

导致甚至更多 ETag 被发送下来:

ETag: "8ee1ce1acf18ca1:0",""

总之,我无法尝试或想到任何方法可以杀死 IIS7 上的 ETag,至少在不编写自定义模块等的情况下是如此。

You would think doing this in the web.config would work to disable ETags in IIS7. But sniffer trace confirms that ETag is sent down anyway.

<httpProtocol>
    <customHeaders>
        <remove name="ETag" />
    </customHeaders>
</httpProtocol>

Using blank doesn't work, either. ETag is sent down anyway.

<httpProtocol>
    <customHeaders>
        <add name="ETag" value="" />
    </customHeaders>
</httpProtocol>

Setting the ETag to blank quotes as other sites have suggested doesn't work.

<httpProtocol>
    <customHeaders>
        <add name="ETag" value="""" />
    </customHeaders>
</httpProtocol>

Causes even more ETag to be sent down:

ETag: "8ee1ce1acf18ca1:0",""

In conclusion, nothing I can try or think of works to kill ETag on IIS7, at least without writing custom modules, etc.

×纯※雪 2024-07-18 19:31:46

我编写了一个自定义 http 模块来处理这个问题。 这确实没有听起来那么糟糕。 这是代码:

using System;
using System.Web;

namespace StrongNamespace.HttpModules
{
    public class CustomHeaderModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.PostReleaseRequestState += new EventHandler(application_PostReleaseRequestState);

        }

        public void Dispose()
        {
        }

        void application_PostReleaseRequestState(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Headers.Remove("Server");
            HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
            HttpContext.Current.Response.Headers.Remove("ETag");
        }
    }
}

这是您需要的 web.config 更改:

<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <remove name="X-Powered-By"/>
            </customHeaders>
        </httpProtocol>
        <modules>
            <add name="CustomHeaderModule" type="StrongNamespace.HttpModules.CustomHeaderModule"/>
        </modules>
    </system.webServer>
</configuration>

I wrote a custom http module to handle this. It's really not as bad as it sounds. Here's the code:

using System;
using System.Web;

namespace StrongNamespace.HttpModules
{
    public class CustomHeaderModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.PostReleaseRequestState += new EventHandler(application_PostReleaseRequestState);

        }

        public void Dispose()
        {
        }

        void application_PostReleaseRequestState(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Headers.Remove("Server");
            HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
            HttpContext.Current.Response.Headers.Remove("ETag");
        }
    }
}

Here's the web.config changes you'll want:

<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <remove name="X-Powered-By"/>
            </customHeaders>
        </httpProtocol>
        <modules>
            <add name="CustomHeaderModule" type="StrongNamespace.HttpModules.CustomHeaderModule"/>
        </modules>
    </system.webServer>
</configuration>
梦屿孤独相伴 2024-07-18 19:31:46

我意识到这是一个老问题,但我在寻找解决方案时遇到了它。 我想我找到了一个合理的答案,我为这个问题

I realize this is an old question, but I came across it while searching for a solution. I think I found a reasonable answer which I posted for this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文