Java (Tomcat):如何配置无 cookie 子域来提供静态内容

发布于 2024-09-02 15:06:31 字数 637 浏览 3 评论 0原文

Google 和 Yahoo! 给出的提示之一!加速网页加载的方法是配置一个无 cookie 的子域来服务器静态内容。

如何在独立模式下使用 Tomcat 配置“无 cookie 子域”(这个问题不是关于如何使用 Apache 以无 cookie 方式提供静态内容,而是关于如何在 Tomcat 独立模式下做到这一点)?

请注意,我不关心支持 If-Modified-Since 的过滤器,也不关心支持 gzipping 的过滤器:我提供的静态内容永远可缓存(或其名称将更改),并且它已经压缩数据(因此 gzip 只会减慢传输速度)。

我需要两个不同的 Tomcat Web 应用程序吗? (一个“cookiefull”和一个“cookieless”)

我需要两个不同的 servlet 吗? (到目前为止,我只有一个调度程序/控制器 servlet)。

为什么“常规”链接(例如,静态图像)在与主 Web 应用程序位于同一域时以 cookiefull 方式调用,然后在子域上时以“无 cookie”方式调用?

我不明白究竟发生了什么:是浏览器决定在查询中附加还是不附加 cookie?如果是这样,为什么它不将 cookie 附加到“无 cookie”子域上的静态查询中。

任何关于幕后发生的事情的例子都是非常受欢迎的:)

One of the tip given by both Google and Yahoo! to speed up webpages loading is to configure a cookieless subdomain to server static content.

How do you configure a "cookieless subdomain" using Tomcat in standalone mode (this question is not about how to use Apache to serve static content in a cookieless-way, but about how to do it in Tomcat-standalone mode)?

Note that I don't care about filters supporting If-Modified-Since nor care about filters supporting gzipping: the static content I'm serving is forever cacheable (or its name will change) and it is already compressed data (so gzip would only slow down the transfer).

Do I need two different Tomcat webapps? (one "cookiefull" and one "cookieless")

Do I need two different servlets? (as of now I've got only one dispatcher/controller servlet).

Why would a "regular" link to, say, a static image be called in a cookiefull way when it would be on the same domain as the main webapp and then be called in a "cookie-less" way when it is on a subdomain?

I don't understand exactly what is going on: is it the browser that decides to append or not cookies to the query? If so, why would it not append the cookies to a static query on a "cookieless" subdomain.

Any example as to what is going on behind the scene is most welcome :)

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

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

发布评论

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

评论(2

三人与歌 2024-09-09 15:06:31

您只需要设置另一个域。它可以指向相同的网络应用程序。只需确保您不会在这个新域上放置 cookie。

例如,您的主站点是

  www.example.com

,您可以

  static.example.com 

为您的无 cookie 静态资源拥有另一个域。它们可以指向相同的主机,转到相同的Servlet。您只需要确保在您的应用程序中不会删除这些静态内容的 cookie。

编辑:假设您的静态内容由 Tomcat 中的默认 servlet 提供,您无需执行任何操作。它不会为您删除 cookie,因此您的新域应该没有 cookie。

如果您必须在同一个 servlet 中处理静态内容,您可以执行类似以下操作,

   if (!request.getServerName().equals("static.example.com")) {
       // Drop cookie
   }

此示例假设您没有在根域 (.example.com) 中删除 cookie。如果您这样做,您必须获得另一个域,例如 examplecdn.com。

You just need to setup another domain. It can point to the same webapps. Just make sure you don't drop cookies on this new domain.

For example, you main site is

  www.example.com

and you can have another domain

  static.example.com 

For your cookie-less static resources. They can point to the same hosts, go to the same Servlet. You just need to make sure in your app that you don't drop cookies for these static contents.

EDIT: Assuming your static content is served by the default servlet in Tomcat, you don't have to do anything. It doesn't drop cookies for you so your new domain should be cookie free.

If you have to process the static content in the same servlet, you can do something like this,

   if (!request.getServerName().equals("static.example.com")) {
       // Drop cookie
   }

This example assumes you don't drop cookie in your root domain (.example.com). If you do that, you have to get another domain, like examplecdn.com.

偏闹i 2024-09-09 15:06:31

我在这里发布了有关此内容的文章: URL/子域重写 (htaccess)

认为您可能会倒退, (或者很可能我会这样做)澄清一下,如果您实现了无 cookie 的子域&具有 www 的基本 URL。至少在这种情况下,cookie 是在 www 上设置的,例如:主要的 cookie 设置者是 google Analytics,因此当在我的网站上设置他们的脚本时,它看起来像这样:

`var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'analytics-acc-#],

['_setDomainName', '[www. valpocreative.com]2'],

['_trackPageview']);

`

您可以在这里看到我将主域设置为 www,请纠正我如果我错了,我需要将 www 重定向到非 www 子域 &不是相反。这也是我的 cpanel 上的 cname 设置(cname=“cdn”指向 www.domain.com)

I posted about this here: URL/Subdomain rewrites (htaccess)

think you may have it backwards, (or very possibly I do) to clarify, if your implementing a cookie-less subdomain & have a base URL of www. Atleast in this case cookies are set on www, for example: a major cookie setter is google analytics, so when setting their script on my site it looks like this:

`var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'analytics-acc-#],

['_setDomainName', '[www.valpocreative.com]2'],

['_trackPageview']);

`

You can see here that i set my main domain to www, correct me if i'm wrong in my case I would need to redirect www to non www subdomain & not the other way around. This is also the cname setup made on my cpanel (cname= "cdn" pointing to www.domain.com)

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