How to make squid proxy server cache response with vary: * in header? - squid

I'm building a system to serve the same page (even though it's not fresh anymore) when requesting the same URL within a run which can be about an hour, so I try using squid cache to cache everything. I add this to squid.conf:
refresh_pattern ^http: 600000 100% 700000 override-expire
override-lastmod reload-into-ims ignore-reload ignore-no-cache
ignore-private ignore-no-store ignore-must-revalidate ignore-auth
However, it doesn't seem to work when the HTTP response has "Vary: *" in the header. For example, I cannot cache http://stackoverflow.com. I'm using squid version 3.1.19, if that matters.
Is there a way to get around this?

"Vary: *" essentially means that there are factors other than headers in the HTTP request that determine the uniqueness of a request (for example, client IP address, etc), so a intermediate cache (squid) cannot really reliably cache.
Unfortunately, Squid has no mechanism for ignoring the Vary header, either completely or for select headers. I'm running into this problem myself.

Related

What does this line mean in rfc2068

source
In addition, the proliferation of incompletely-implemented
applications calling themselves "HTTP/1.0" has necessitated a
protocol version change in order for two communicating applications
to determine each other's true capabilities.
From the RFC:
HTTP has been in use by the World-Wide Web global information initiative since 1990. The first version of HTTP, referred to as HTTP/0.9, was a simple protocol for raw data transfer across the Internet.
Rephrased:
Before HTTP was standardised there were differences in implementations that meant they couldn't always communicate with each other correctly (e.g. certain web-browsers couldn't work with certain web-servers). The RFC article refers to these pre-standardisation implementations as using HTTP/0.9.
HTTP/1.0, as defined by RFC 1945, improved the protocol by allowing messages to be in the format of MIME-like messages, containing metainformation about the data transferred and modifiers on the request/response semantics. However, HTTP/1.0 does not sufficiently take into consideration the effects of hierarchical proxies, caching, the need for persistent connections, and virtual hosts. In addition, the proliferation of incompletely-implemented applications calling themselves "HTTP/1.0" has necessitated a protocol version change in order for two communicating applications to determine each other's true capabilities.
Rephrased:
After HTTP was standardised as HTTP/1.0 it certainly helped the interopability and compatibility problems, but version 1.0 of the protocol simply assumed all HTTP software would be able to use it for their existing application, but now that HTTP/1.0 has been in-use for a while the maintainers of the HTTP protocol specification saw that they need to extend HTTP to support these use-cases (e.g. proxies, caches, persistent connections, virtual-hosts) and while these things could be done using the built-in extension mechanisms in HTTP/1.0 they felt a need to increment the version number to HTTP/1.1 in order to prevent an implementation simply assuming the remote host supports a feature or not.
Example
A good example is the Host header in HTTP/1.1 that allows for a web-server serving from a single IP address and port number to serve-up different websites based on the Host header (as before HTTP/1.1 existed webservers could only serve one website per IP address, which is a problem). HTTP/1.0 does allow clients and servers to add their own custom headers, such as Host, however there is no way for the client or the server to know that the other end actually supports the Host header. But in HTTP/1.1 the Host header was formerly added to the specification so if both the client and server declare they use HTTP/1.1 then the other end knows that they'll recognize the Host header and handle it correctly.
So in the HTTP/1.0 days, with custom headers, this is how it would play out if a browser requests www.example.com if it were served from a Shared Webhost:
Browser (to DNS server): "Please give me the IP address for 'www.example.com'"
DNS Server (to browser): "www.example.com is 198.51.100.7"
Browser (to 198.51.100.7): "Hello, I speak HTTP/1.0, please send me index.html for Host: www.example.com
Server (to browser): "I also speak HTTP/1.0, here is index.html for 'not-actually-example.com'"
As you can see, the browser got not-actually-example.com even though it asked for www.example.com, because the Web-server was using HTTP/1.0 which does not recognize the Host header, even though the web-browser was sending the Host header (as an extension/experimental header). The browser software has no way of knowing if not-actually-example.com is what the user wanted or not.
In human terms, what they're saying is: so many people said they did HTTP 1.0 while they didn't, that nobody knew whether it really was HTTP 1.0 any more when someone said it.
To get out of that, they chose a new number.

Cloudflare not respecting edge cache TTL value

Scenario
I have a domain static.example.com. All resources under that domain are write-once, read-many. I want Cloudflare to cache its resources as aggressively as possible; if a resource ever changes on the server, the only means by which a client should be able to fetch the updated version is if I manually go into Cloudflare and clear the cache for that resource.
Now, I understand that this is not possible. The next best thing I can do to make this happen on the server (not with client-side caching e.g. with Cache-Control headers) is set the edge cache TTL as high as possible.
The problem
I have configured a Page Rule in Cloudflare as follows:
*static.example.com/*
Cache Level: Cache Everything, Edge Cache TTL: a month
However, Cloudflare does not seem to be respecting the edge cache TTL of one month.
Reproduction steps
Actual behavior
GET https://static.example.com/img.png. Response header cf-cache-status: MISS. Load is slow, since it goes to my origin server.
GET https://static.example.com/img.png, from the same IP. Response header cf-cache-status: HIT. Load is fast, since it is cached by Cloudflare.
Wait one day, during which I do not make any additional requests to static.example.com.
GET https://static.example.com/img.png, from the same IP. Response header cf-cache-status: MISS. Load is slow! (Why wasn't the edge cache TTL of one month respected? The resource should not have been purged from the Cloudflare cache!)
Expected behavior
GET https://static.example.com/img.png. Response header cf-cache-status: MISS. Load is slow, since it goes to my origin server.
GET https://static.example.com/img.png, from the same IP. Response header cf-cache-status: HIT. Load is fast, since it is cached by Cloudflare.
Wait one day, during which I do not make any additional requests to static.example.com.
GET https://static.example.com/img.png, from the same IP. Response header cf-cache-status: HIT. Load is fast.
Question
Why is the edge cache TTL value I set not respected by Cloudflare's proxy servers?
It's just a cache. Expiration is just advice on when to definitely expire it, but like with all caches - you can not trust anything to be cached - or to be cached for the whole period. The item is most likely expired from their cache with last recently used logic, and any content that is popular will push your image out of the cache. If you want a guarantee for elements being on a CDN, you should not use a proxy based CDN like Cloudflare is.

What and Where are Intermediary HTTP Caches

I see that one of the big benefits of REST is relying on HTTP caching. I'm not arguing with this and completely buy into the idea. However, I never see a deeper explanation of intermediary HTTP caches.
If I set the Cache-control header to "public, max-age=86000" or any other max-age that would cause a response to be cached, where would it be cached? As far as I can tell it would be cached by the browser. I also hear that ISPs have caches.
So what kinds of intermediary cache are there, and how likely is a response from my web server to be cached if I set the cache-control header as above?
If you are on Windows it may be cached by WinInet proxy cache, depending on what application is running. On a corporate network, there maybe a cache in your corporate proxy. Your ISP may have a cache. Products like Squid, Varnish, ngnix are used as HTTP intermediary caches.
It is impossible to say what chance there is of you hitting a cache when accessing your server, unless you put one there yourself.

What HTTP client headers should I use to instruct proxies to refetch from origin, and cache the response?

I'm currently working on a system where a client makes HTTP 1.1 requests of an origin server. I control both the client and the server software, so have free reign over HTTP headers set. Between the client are multiple, hierarchical layers of web proxy / cache devices (think, Squid or similar).
The data served up by the origin is usually highly cacheable, and I intend to set HTTP response headers to indicate this. Specifically, I plan to use Cache-Control: public, max-age=<value>. I understand that this will mean that intermediate proxies will cache the response up to the specified max-age, at which point they will revalidate against the origin (presumably with a Last-Modified header, looking for a 304 response).
The problem I have is that the client might become aware that the data held by caches might now be invalid. In this case, I need the client to make a request which instructs the caches to either fetch or revalidate their response with the origin. If the origin response is now different, the cache should store this new response. In my mind, this would involve the client making the request, and each cache in the chain should revalidate its response with the next upstream device, all the way back to the origin. The new response can then be served from the closest cache which actually has it.
What's the correct HTTP headers that need to be set on the client request to achieve this? At first I thought that setting Cache-control: no-cache in the HTTP request would make this happen, but reading the RFC, it seems that this will instruct the intermediate caches to both go back to the origin (desired) but also not cache the new response (not desired). I then saw an article in which an HTTP request header of Cache-control: max-age=0 would perhaps do this, but I'm not sure.
Will max-age=0 do what I need here, or do I need some other combination of HTTP headers?
I asked a similar question here: How to make proxy revalidate resource from origin. I since learned that proxy revalidate wasn't supported by nginx at the time of writing. It is scheduled for the 1.5 release.
Sending max-age=0 from the client should trigger this revalidate mechanism in the proxy, if the original response from the origin contained the right cache control headers.
But whether your upstream server(s) will respect these headers and revalidate with their origin is clearly not something you can just assume. If you have control over your upstream servers I think it could work.
Also etag is preferred over modified since headers afaik.
I found these to be helpful articles on the subject:
caching tutorial
cache control directives
http specs on validation
section 14.9.4 on this spec
[UPDATE]
Nginx version 1.5.8 has been released since, and I can confirm that this mechanism is now working!

Varnish + Static HTML Pages

I've recently come across a http web accelerator called Varnish. From what I've read, Varnish speeds up delivery of a website by optimizing every process of HTTP communication with the HTTP server using a reverse proxy configuration.
My question is that if you have a website that has its caching mechanism configured all the way down to static html files then how much more of an effect will Varnish have on this? Does a reverse proxy cut down the work that is performed by the HTTP server to process the request? If you have everything extensively cached on the server-side (HTTP headers, Etags, Expires Headers, Database Caching, Fragment and Page caching) then what more will a HTTP accelerator do to improve on this?
Firstly, we should differentiate between two different types of caching that go on in a normal web system: HTTP caching and server-side caching.
HTTP caching is controlled by HTTP headers, notably as you point out ETag and the various expiry mechanisms (including Expires and various aspects of Cache-Control). This is all covered in RFC 2616 (HTTP), section 13, and allows HTTP caches to return a response to an HTTP request from a client without having to go back to the origin server. In effect, the HTTP caching mechanism allows another machine between client and server to act as if it's the server, in certain cases. This is actually what varnish is doing, as we'll see in a minute; another common use that many people are familiar with is when ISPs provide an HTTP cache within their network, that can generally respond faster to their subscribers (and so improve perceived performance) than the origin servers outside their network.
Server-side caching includes database caching, and fragment and page caching, which are really all just ways of the web server avoiding doing some expensive operation (say, a database query, or rendering a particular piece of a template) by doing it once then keeping the result in a cache for a while.
I said earlier that varnish was an HTTP cache, which means that straight away it's able to be more efficient than a web server serving even a static file. Consider what a web server has to do:
parse the HTTP request
map the URI (and any relevant request headers, such as Accept-Encoding) onto a file
pull up information about the file to build the HTTP headers in the response; these are known as entity headers (RFC 2616 section 7.1, which include things such as Content-Length, Content-Type and the Expires and Last-Modified headers used in HTTP caching)
figure out what additional response headers (RFC 2616 section 6.2; these include ETag and Vary, both important parts of HTTP caching) and general header fields (RFC 2616 section 4.5) are needed
write the HTTP status line and headers out to the network
write the file's contents out to the network
By comparison, varnish is upstream of all of this, so all it has to do is:
parse the HTTP request
map the URI (and any relevant request headers) onto an entry in its internal cache
see if there's an entry; if there is, write it to the network; the HTTP headers will have been stored in the cache
If there isn't an entry, varnish has to do a little more work:
connect to a web server behind it that will run through all the steps 1-6 in the first list to generate a response
write the response to the network, including all the HTTP headers
store the response in its cache
In particular because the HTTP headers and entity body (the entire response) can be cached by varnish, if it can serve out of its cache it has less work to do. When you start generating the response dynamically in your server, the difference can become even more pronounced: say you have a page that takes 5 seconds to generate, but is the same for everyone hitting your site, varnish should be able to serve that in at most milliseconds out of the cache (plus whatever time it takes to get the response across the network to the HTTP client), and has a neat mechanism (the grace period) so it can keep on doing it while hitting the backend server once to refresh the cached version of the page.
Of course, you can introduce server-side caching to improve the speed with which your web server can process a request, but if you have a response you can cache in varnish it's generally going to be faster to do that. (There are various things that are hard to cache in varnish, particularly if you're using cookies or have pages that change depending on which user is looking at them. While it's possible to continue using varnish in these cases, unless you need really incredible speed, as far as I'm aware most people start optimising those cases using server-side caching and other techniques before hitting up varnish.)
(Note that varnish can also edit headers and indeed data going in and out of the cache, which complicates things. But the main points still stand, and even while editing things on the fly varnish can be incredibly fast.)

Resources