asp.net - 停止从域(又名 "cookieless domain")设置 cookie 以提高站点性能

标签 asp.net cookies windows-server-2008 iis-7.5

我正在阅读Google's documentation关于提高网站速度。他们的建议之一是从“无 cookie 域”提供静态内容(图像、CSS、JS 等):

Static content, such as images, JS and CSS files, don't need to be accompanied by cookies, as there is no user interaction with these resources. You can decrease request latency by serving static resources from a domain that doesn't serve cookies.

Google 表示,最好的方法是购买一个新域名并将其设置为指向您当前的域名:

To reserve a cookieless domain for serving static content, register a new domain name and configure your DNS database with a CNAME record that points the new domain to your existing domain A record. Configure your web server to serve static resources from the new domain, and do not allow any cookies to be set anywhere on this domain. In your web pages, reference the domain name in the URLs for the static resources.

这是非常简单的内容,除了它说“配置您的 Web 服务器以提供来自新域的静态资源,并且不允许在此域上的任何位置设置任何 Cookie”。 From what I've read ,IIS 中没有设置允许您说“提供静态资源”,那么如何防止 ASP.NET 在这个新域上设置 cookie?

目前,即使我只是从新域请求 .jpg,它也会在我的浏览器上设置一个 cookie,即使我们应用程序的 cookie 设置为我们的旧域。例如,ASP.NET 设置了一个“.ASPXANONYMOUS”cookie(据我所知)我们并没有告诉它这样做。

抱歉,如果这是一个真正的新手问题,我是新手!

谢谢。

最佳答案

这就是我在网站中所做的:

  1. 使用 ASP.NET 应用程序池在 IIS 上设置网站
  2. 将绑定(bind)主机设置为 your.domain.com
    • 注意:您不能使用domain.com,否则子域将不会是无cookie的
  3. 在网站上创建一个名为 Static 的文件夹
  4. 设置另一个网站,将其指向之前创建的 Static 文件夹。
  5. 将绑定(bind)主机设置为 static.domain.com
  6. 使用包含非托管代码的应用程序池
  7. 在设置中打开“ session 状态”并选中未启用

现在您有了一个静态网站。要进行设置,请打开 Static 文件夹下的 web.config 文件并替换为以下文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <sessionState mode="Off" />
    <pages enableSessionState="false" validateRequest="false" />
    <roleManager>
      <providers>
        <remove name="AspNetWindowsTokenRoleProvider" />
      </providers>
    </roleManager>
  </system.web>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
    </staticContent>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

这会将文件缓存 30 天,删除 RoleManager(我不知道它是否会改变任何内容,但我删除了我能找到的所有内容),并从响应 header 中删除了一个项目。

但是这里有一个问题,即使部署新版本,您的内容也会被缓存,因此为了避免这种情况,我为 MVC 制作了一个辅助方法。基本上,您必须附加一些查询字符串,每次更改这些文件时这些查询字符串都会更改。

default.css?v=1   ?v=2  ...
<小时/>

我的 MVC 方法获取最后写入日期并附加到文件 url 上:

public static string GetContent(this UrlHelper url, string link)
{
    link = link.ToLower();

    // last write date ticks to hex
    var cacheBreaker = Convert.ToString(File.GetLastWriteTimeUtc(url.RequestContext.HttpContext.Request.MapPath(link)).Ticks, 16);

    // static folder is in the website folders, but instead of
    // www.domain.com/static/default.css I convert to
    // static.domain.com/default.css
    if (link.StartsWith("~/static", StringComparison.InvariantCultureIgnoreCase))
    {
        var host = url.RequestContext.HttpContext.Request.Url.Host;
        host = String.Format("static.{0}", host.Substring(host.IndexOf('.') + 1));

        link = String.Format("http://{0}/{1}", host, link.Substring(9));

        // returns the file URL in static domain
        return String.Format("{0}?v={1}", link, cacheBreaker);
    }

    // returns file url in normal domain
    return String.Format("{0}?v={1}", url.Content(link), cacheBreaker);
}

并使用它(MVC3 Razor):

<link href="@Url.GetContent("~/static/default.css")" rel="stylesheet" type="text/css" />

如果您使用的是另一种应用程序,您可以执行相同的操作,创建一个在页面上附加 HtmlLink 的方法。

关于asp.net - 停止从域(又名 "cookieless domain")设置 cookie 以提高站点性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3060775/

相关文章:

javascript - 将输入的文本保存到 cookie 并在用户每次访问时显示给用户

mysql - 是否可以将本地 MySQL 数据库复制到远程 MySQL 数据库?

Android Jsoup 向 ASPX 页面发布请求总是出现 500 错误

asp.net - 如何使用 Windows 身份验证指定 Windows 用户从 ASP 连接到 MS SQL Server 数据库

javascript - jquery.cookie.js mod_security 错误 406 和问号

php - 正确设置curl php cookie

c# - 导出到 excel - Windows Server 2008 IIS7 上的问题

.net - 如何在 Windows Server 2008 的程序集中安装文件?

javascript - 使用 Javascript 在嵌套 FormView 中隐藏/显示控件

javascript - 如何将参数从 ListView 中的项目传递到模板