Skip to content

Squid as a reverse proxy — accelerator mode

Run Squid in HTTP accelerator (reverse proxy) mode in front of origin servers — listener, cache_peer, refresh patterns, and where it still earns its keep.

Squid’s reverse-proxy mode is a niche we still pick deliberately. Varnish or Nginx will be the default answer for most “cache in front of an origin” jobs, but Squid in accel mode wins on two specific cases: legacy compatibility with ICP/HTCP federations, and rich refresh_pattern policies on object expiry. This article is the install of a reverse Squid we have run in production, with the trade-offs that pushed us to choose it rather than something newer.

How to verify

Confirm what is currently in front of origin, what ports Squid will bind, and whether the origin will see the original client IP.

ss -ltnp | grep -E ':(80|3128|443)\b'
curl -I -H "Host: app.example.com" http://localhost:80/ | head
squid -v | head -5
grep -E 'http_port|cache_peer|refresh_pattern' /etc/squid/squid.conf

If a different cache is on port 80 already, decide the migration plan before reconfiguring the listener — there is no “Squid will quietly take over.”

What’s happening

In forward-proxy mode Squid talks to many destinations on behalf of internal clients. In reverse-proxy (“accelerator”) mode Squid talks to one or a few defined origins on behalf of arbitrary public clients. The listener is configured with http_port 80 accel defaultsite=app.example.com, origins are declared as cache_peer, and ACLs select which peer handles which request.

Squid’s caching policy is governed by refresh_pattern directives — regex-matched rules that say “for URLs matching this pattern, the minimum freshness is X minutes, maximum is Y, and use these heuristics when origin sends no Cache-Control.” This is more expressive than most modern caches and is the main reason a few of our clients still run Squid: their origins emit no useful cache headers and the only way to cache them at all is to make policy decisions at the cache, not the application.

For HTTPS, Squid needs the TLS certificate; the common pattern is to terminate TLS at HAProxy or Nginx and have Squid speak plain HTTP behind it. Running Squid as the TLS terminator is possible but the ergonomics are weaker than the alternatives.

The procedure

  1. Lay down a reverse-proxy config. The directive accel tells Squid this listener is for reverse traffic, not forward.

    cat > /etc/squid/squid.conf <<'EOF'
    http_port 80 accel defaultsite=app.example.com no-vhost
    visible_hostname cache.internal
    
    cache_peer origin-1.internal parent 8080 0 no-query originserver name=origin_1
    cache_peer origin-2.internal parent 8080 0 no-query originserver name=origin_2
    
    acl origin_sites dstdomain app.example.com www.example.com
    http_access allow origin_sites
    http_access deny all
    
    cache_peer_access origin_1 allow origin_sites
    cache_peer_access origin_2 allow origin_sites
    
    cache_dir aufs /var/spool/squid 10000 16 256
    cache_mem 1024 MB
    maximum_object_size 64 MB
    
    refresh_pattern \.(?:css|js|jpg|jpeg|png|gif|svg|woff2)$ 1440 90% 10080 override-expire
    refresh_pattern -i ^http://app\.example\.com/api/    0    0%   0
    refresh_pattern .                                    60   20%  4320
    
    forwarded_for transparent
    via off
    reply_header_access X-Cache deny all
    access_log daemon:/var/log/squid/access.log combined
    EOF
  2. Provision the cache directory. cache_dir needs initialisation before Squid will start.

    mkdir -p /var/spool/squid && chown -R proxy:proxy /var/spool/squid
    squid -z                                # initialise the swap structure
    systemctl restart squid
  3. Wire origin to see client IP. With forwarded_for transparent, Squid passes the client IP via X-Forwarded-For; origin still sees Squid as the connection peer.

    curl -I -H "Host: app.example.com" http://cache.internal/
    # on origin, in the application log:
    tail -n 5 /var/log/app/access.log | awk '{print $1, $7}'
  4. Validate the refresh patterns. Squid evaluates refresh_pattern top-to-bottom, first match wins. Mistakes here are silent — the cache simply behaves wrong.

    squid -k parse
    curl -I http://cache.internal/static/main.css | grep -i 'x-cache\|age\|expires'
    # Hit the same URL twice; second should show "Age: <small_number>"
  5. Plumb the cache manager. cachemgr.cgi (or HTTP-direct via mgr: URLs) exposes runtime stats. Restrict it tightly.

    echo 'http_access allow localhost manager' >> /etc/squid/squid.conf
    echo 'http_access deny manager' >> /etc/squid/squid.conf
    squidclient mgr:info | head -40
    squidclient mgr:objects | head

Common pitfalls

  • Forgetting no-vhost and seeing every request rewritten to defaultsite regardless of Host header.
  • Setting cache_dir too large for the disk — Squid does not pre-allocate; it discovers the disk is full at the worst moment.
  • Letting refresh_pattern defaults (in the shipped config) match before yours — the order matters and the default lines should be deleted.
  • Mixing accelerator and forward-proxy modes on the same instance — the ACLs get tangled and a misconfigured rule lets internal clients use the public cache as an open proxy.
  • Running Squid behind a load balancer that strips X-Forwarded-For — origin sees Squid IPs for every request and rate-limits its own cache.

We choose Squid in reverse mode only when a client has a long history with it and a working refresh_pattern policy nobody wants to re-derive in Varnish. For new builds we lean Varnish or Nginx with a managed-cache module — see our HAProxy + Varnish stack write-up. Either way, Stack Harbor’s role in managed operations is to own the cache as a first-class component, not bolt it on after the fact.