Mixing varnish/WordPress & Rails on a single droplet

March 4, 2013 3.1k views
I've got one droplet, currently set up to serve both WordPress via nginx/php5-fpm and Rails via passenger. I'm trying to set varnish up in a way where it's only being used for the WordPress sites, but not for the Rails sites. I followed the guide in the tutorial setup section, and here's my config setup: https://gist.github.com/jschuur/6aae550e0d0d1d607729 Any attempt to access http://joostschuur.com (WordPress) just loads up the first site configured, i.e. hellorails.joostschuur.com (Rails). It looks like varnish is passing something along to nginx, nginx goes 'Err, don't know that server/port, here, have the default one instead'. I have tried playing around with the listen directive. using from the tutorial e.g. didn't help. The setup I want is for varnish to immediately pass any Rails site I'm accessing to nginx, and handle caching for any WordPress sites as needed. So I think my current problem isn't even related to varnish yet. It's purely an nginx config issue, getting multiple server blocks to listen on different ports. After that's solved, I'm going to need to have all the nginx sites (even the Rails ones) run on prt 8080 presumably. (Note that right now, I have Varnish disabled until I figure this out, so joostschuur.com loads up just fine again, just with no caching).
1 Answer
Answering my own question here... I'm actually not entirely sure how I ended up fixing it. I switched all the server entries in nginx over to listen at 8080, restarted nginx and varnish a few times during testing, tried a few various things, and now it seems to work.

For a while, it sounded like I might need to use the nginx's port_in_redirect option, or explicitly tell WordPress that the WordPress Address (URL) ends in port 8080, but after I removed both of those again, it's still working.

Here's what I ended up putting in my default.vcl, to have varnish skip trying to handle anything but my 2 WordPress sites (i.e. the Rails apps, which tend to be on subdomains off of joostschuur.com) on the droplet:

sub vcl_recv {
# Skip Rails sites
if (req.http.host ~ "martiansoil.com" ||
req.http.host == "www.joostschuur.com" ||
req.http.host ~ "^joostschuur.com" ) {
# Drop any cookies sent to Wordpress.
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
} else {
return (pass);

FWIW, pages are being served 4-5 times faster with varnish according to AB. Not super great, but not bad either.
Have another answer? Share your knowledge.