Search code examples
nginxvarnishmod-pagespeed

ngx_pagespeed behind Varnish, how specify multiple (or correct) DownstreamCachePurgeLocationPrefix?


We use Google PageSpeed module with Nginx behind Varnish.

Pagespeed only delivers partially optimized page on first hit and we use this config to make Pagespeed send purge requests to Varnish so that Varnish will clear that page once Pagespeed does indeed have a fully optimized version:

pagespeed DownstreamCachePurgeLocationPrefix http://10.128.1.1:6081;
pagespeed DownstreamCachePurgeMethod PURGE;
pagespeed DownstreamCacheRewrittenPercentageThreshold 95;

Problem is we have multiple Varnish sitting in front of multiple pagespeed receptors and varnish directs traffic in round robin.

So if app-05 for example gets the traffic, there's no telling which varnish proxy cached that page. What nginx variable can I use to tell pagespeed what varnish server cached the first partially optimized page?


Solution

  • To send purge request back to correct varnish server. On Varnish side I put in vcl_recv:

    set req.http.X-Forwarded-From = server.ip;
    

    And then on Nginx I put:

    pagespeed DownstreamCachePurgeLocationPrefix http://$http_x_forwarded_from:6081;
    

    I thought this would work but it doesn't seem to be.

    I know the variable is getting populated with Varnish ip in Nginx (because I echoed it via a 200 response) but for whatever reason pagespeed doesn't seem to want to accept the ip address from this variable.