Search code examples
reverse-proxysquid

Squid 2.7 and the client header max-age=0


I'm trying to use squid as a reverse cache proxy in front of a php application in the same machine and never let the user to reach the backend, always load stale cache in background.


I'm using a self compiled squid 2.7-Stable9 with Centos 6.3 (x86_64) in the port 80 and the apache+php is in the same machine with the port 8000.

The apache+php response header sends this to squid:

header('Cache-Control: max-age=10, stale-while-revalidate=15, stale-if-error=20');
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

When I ask for a page to squid, first time revalidates and next time uses always stale, but this works with the DISABLE CACHE cache option of Firefox WebDevelopersTools extension.

The difference is that when cache is not disabled, client sends Cache-control: max-age=0 and If-Modified-Since and squid connects to the backend always and revalidates.

In Squid 3.2 it worked as expected using ignore-cc but in 2.7 there is not such option.

Here is my squid.conf:

# http_port public_ip:port accel defaultsite= default hostname, if not provided
http_port 80 accel defaultsite=mydomain.com

# IP and port of your main application server (or multiple)
cache_peer 127.0.0.1 parent 8000 0 no-query originserver name=main
cache_peer_domain main mydomain.com

# Do not tell the world that which squid version we're running
httpd_suppress_version_string on

# Remove the Caching Control header for upstream servers
header_access Cache-Control deny all

# log all incoming traffic in Apache format
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
access_log /usr/local/squid/var/logs/squid.log combined all

cache_effective_user squid

refresh_pattern nocache=true 0 0% 0

icp_port 0

I want to select the cache time in the php application and never let the user to see the latency of revalidating, that's why I'm using version 2.7 with stale-while-revalidate.


Example of request with cache enabled:

GET /some-page HTTP/1.1
Host: mydomain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1 FirePHP/0.7.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
x-insight: activate
If-Modified-Since: Thu, 11 Oct 2012 10:14:52 GMT
Cache-Control: max-age=0

HTTP/1.0 200 OK
Date: Thu, 11 Oct 2012 10:29:31 GMT
Server: Apache/2.2.22 (PowerStack)
Last-Modified: Thu, 11 Oct 2012 10:29:32 GMT
Content-Type: text/html; charset=UTF-8
X-Cache: MISS from localhost
X-Cache-Lookup: HIT from localhost:80
Via: 1.1 localhost:80 (squid)
Connection: close

Example with cache disabled (what I want to happen):

GET /some-page HTTP/1.1
Host: mydomain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1 FirePHP/0.7.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
x-insight: activate

HTTP/1.0 200 OK
Date: Thu, 11 Oct 2012 10:36:53 GMT
Server: Apache/2.2.22 (PowerStack)
Last-Modified: Thu, 11 Oct 2012 10:36:53 GMT
Content-Type: text/html; charset=UTF-8
Age: 2
Content-Length: 16725
X-Cache: HIT from localhost
X-Cache-Lookup: HIT from localhost:80
Via: 1.1 localhost:80 (squid)
Connection: keep-alive

Solution

  • Solution described here: How to use in squid 2.7 the 3.1 feature "ignore-cc"


    Well, after trying all alternatives, I modified the source code to implement the feature.

    I changed in the Squid-2.7-stable9 the file src/refresh.c:282:

    282:    if (request) {
    

    for

    282:    if (request && FALSE) {
    

    They do something like that in the 3.X to implement ignoreCc:

    269:    if (request && !request->flags.ignore_cc) {
    

    Worked as expected.