I generate htm files dynamically usin开发者_如何学Cg php and .htaccess. I read somewhere that I should remove Etags for files of type text/html? Is that correct? I am wondering if I use etags and If i don't change the content, I could save some bandwidth. I would appreciate if you guys could tell me if I can use etags for htm files.
As far as i know the Etag is an http header is generated by the HTTP server used by the cache system.
The idea:
- You ask to stackoverflow.com the image logo.png
- stackoverflow.com will answer to you with a HTTP 304 (content not modified, etag: XXXXXX)
- Your browser before ask the image again will check the cache for a resource called: logo.png, from the website: stackoverflow, with the etag: XXXXXXX
- If the browser find it, it will load the image from the cache, without downloading
- If it can't find it, it will ask again to the web server to download it.
so... for what propose you want use the ETags ?
If you want understand more about the ETags could be interesting download HttpFox for firefox.
Apache have his own cache system and it's used when you download or require any "static" download, like html files and images.
If you want do it on dynamic context you must implement it by yourself.
Etags can be usefull speeding up your website, even with dynamic content (like php scripts). Especially on mobile connections this is important, since connection speed is slower. I use ETag headers on some mobile websites like this:
https://gist.github.com/oliworx/4951478
Hint: You must not include curent time or other often changing content in the page, because this prevents it from beeing cached by the client (the browser).
Remove Etags if possible
The best cache method is max-age. W3C mandates that Browsers must use max-age if available.
When max-age is used the Browser will use the cached version and not even query the Server.
This also means if you are replacing a resource on you web page (e.g. CSS, JS, IMG, link), you should rename the resource.
The next best caching method is Expires.
In every PHP page with an echo
it is not a bad idea to always include a max-age
header.
header('Cache-Control: max-age=31536000');
These are wise also, (Example Content Type is only for HTML)
header('Content-Type: text/html; charset=utf-8');
header('Connection: Keep-Alive');
header('Keep-Alive: timeout=50, max=100');
eTag has no expiration. The resource must be checked every time.
If you are using max-age or Expires, the Browser will not make an HTTP request to check the resource.
When included with max-age and or expires, it is a waste of hearer space and wastes a few Server CPU cycles to generate or lookup the eTag Value.
The problem with eTag is unless the resource is very large it will have little benefit. In an HTTP Request, the time required for Transmission of the data is often minimal compared to the connect and wait times.
With eTag the Browser still has to do an HTTP Request. When the eTag has not changed, then then the response is 304.
Here is a typical HTTP request:
Only 3 milliseconds to download 2.9KB
454 milliseconds request time. + 58ms DNS (very fast)
DNS Lookup: 58 ms
Initial Connection: 192 ms
Time to First Byte: 262 ms
Content Download: 3 ms
Bytes In (downloaded): 2.9 KB
eTag would save 3 milliseconds.
If resource was cached, it would have freed the connection for another resource in addition to saving the 400-500 ms.
Here is a 301 response from Intel
441 ms
DNS Lookup: 103 ms
Initial Connection: 219 ms
Time to First Byte: 222 ms
Content Download: ms
Bytes In (downloaded): 0.1 KB
精彩评论