Issue link: https://read.uberflip.com/i/1146286
Proxy Caching | 33 Proxy Caching The configuration described so far works fine when the browser talks directly to the web server. The web server determines whether to compress the response based on Accept-Encoding. The browser caches the response, whether or not it has been com- pressed, based on other HTTP headers in the response such as Expires and Cache- Control (see Chapter 3). When the browser sends the request through a proxy it gets more complicated. Sup- pose that the first request to the proxy for a certain URL comes from a browser that does not support gzip. This is the first request to the proxy, so its cache is empty. The proxy forwards that request to the web server. The web server's response is uncom- pressed. That uncompressed response is cached by the proxy and sent on to the browser. Now, suppose the second request to the proxy for the same URL comes from a browser that does support gzip. The proxy responds with the (uncompressed) contents in its cache, missing the opportunity to use gzip. The situation is worse if the sequence is reversed: when the first request is from a browser that supports gzip and the second request is from a browser that doesn't. In this case, the proxy has a compressed version of the contents in its cache and serves that version to all subse- quent browsers whether they support gzip or not. The way around this problem is to add the Vary header in the response from your web server. The web server tells the proxy to vary the cached responses based on one or more request headers. Because the decision to compress is based on the Accept- Encoding request header, it makes sense to include Accept-Encoding in the server's Vary response header. Vary: Accept-Encoding This causes the proxy to cache multiple versions of the response, one for each value of the Accept-Encoding request header. In our previous example, the proxy would cache two versions of each response: the compressed content for when Accept- Encoding is gzip and the uncompressed content for when Accept-Encoding is not specified at all. When a browser hits the proxy with the request header Accept- Encoding: gzip it receives the compressed response. Browsers without an Accept- Encoding request header receive the uncompressed response. By default, mod_gzip adds the Vary: Accept-Encoding header to all responses to provoke the right behavior from the proxy. For more information about Vary, visit http://www.w3.org/Protocols/ rfc2616/rfc2616-sec14.html#sec14.44.

