nginx conditional redirection to php

I recently spent a few hours struggling with nginx to recreate a previous apache rewrite rule. The goal was that
for requests with a specific prefix, where the file is not found, redirect the request to a php script to attempt automatic thumbnail generation.

My eventual location block looked like

location ^~ /assets/ {
try_files $uri /assets/thumbmaker.php?src=$uri;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/assets/thumbmaker.php
fastcgi_param PATH_INFO $uri;
if (!-e $request_filename){
fastcgi_pass php;
}
expires max;
log_not_found off;
}

Published
Categorized as Online