Good way to choose between backends by Varnish depending on requested file existence
OK, if anyone is interested in solution I found a new one with inline C in VCL config. First of all we must add a function to check if file exists (add somewhere in the top of your file outside of any function):
C{#include <stdio.h>#include <stdlib.h>int exists (char *fname){ FILE *file; if (file = fopen(fname, "r")) { fclose(file); return 1; } return 0;}}C
I know that there are better ways to check if file exists, but major headers are not available inside VCL :/
Then in vcl_recv
subroutine add following code:
C{ if( exists("/local/file/path") == 1 ) { VRT_l_req_backend(sp, VCL_conf.director[1]); } else { VRT_l_req_backend(sp, VCL_conf.director[2]); }}C
Works like a charm.
You're using Varnish for something it wasn't made for. It indeed has no possibility to check if a file exists, because it was not made to serve files in the first place. Varnish just temporarily caches back-end responses in file or memory storage.
Do you really need varnish in this setup? Wouldn't it make a lot more sense to have nginx check if the file exists or else forward it to your processor?
Why not use Nginx's try_files directive to silently proxy the request to Apache on a 404? It would seem more logical, to me at least.