Java (Tomcat): how to set up cookieless subdomain to serve static content

One of the tips provided by both Google and Yahoo! to speed up the loading of web pages, you must configure cookieless subdomain on the static content of the server.

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

Please note that I do not need filters that support If-Modified-Since , and do not care about filters that support gzipping: the static content that I serve is cached forever (or its name will change), and this is already compressed data (so gzip only slow down the transmission).

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

Do I need two different servlets? (at the moment I have only one dispatcher / servlet manager).

Why is a β€œregular” link to, say, a static image called in a cookiefully way when it is in the same domain as the main webapp, and then called in cookie-less mode when it is on a subdomain?

I don’t exactly understand what is happening: a browser that decides to add or not cookies to the request? If so, why not add cookies to the static request in the cookieless subdomain.

Any example of what happens behind the scenes is welcome :)

+4
source share
2 answers

You just need to configure another domain. It can point to the same webapps. Just make sure that you do not drop cookies in this new domain.

For example, the main site

  www.example.com 

and you can have another domain

  static.example.com 

For your static resources without cookies. They can point to the same hosts, go to the same servlet. You just need to make sure that in your application you are not deleting cookies for this static content.

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

If you need to handle static content in the same servlet, you can do something like this,

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

This example assumes that you are not throwing a cookie to the root domain (.example.com). If you do this, you need to get another domain, for example examplecdn.com.

+3
source

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

Think that you may have this in the opposite direction (or perhaps I will) to clarify if you use a subdomain without cookies and have a base URL of www. At least in this case, cookies are set to www, for example: the main cookie is Google Analytics, so when setting up their script on my website 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 primary domain to www, correct me, if I am mistaken in my case, I will need to redirect www to not a www subdomain, and not vice versa. This is also the cname setting made on my cpanel (cname = "cdn" pointing to www.domain.com)

+2
source

Source: https://habr.com/ru/post/1310538/


All Articles