File Checking Rewriting and Handing off to Back End

Hi All,

We’re starting to make more and more heavy use of Nginx having shifted
from Apache, mostly in handing off to backends running Puma and php-fpm.
However, wherever possible we naturally want Nginx to serve what it can
with only the bare minimum hitting the back end. However, this generally
means writing more and more complex rules.

I have a requirement for Nginx to serve a file if it exists or hand off
to a Rails controller. The URI is data?x=X&y=Y&z=Z which at the moment
goes direct to a Rails controller, sends the file back directly and
saves a static png file in the process. What I need to do in Nginx is
take the parameters of the URI and check whether the file
/data/X_Y_Z.png exists. If not hand off to the Rails controller. This
means that Nginx serves files and only hands off to Rails when needed.
It also means that I need to formulate the file that I want to check
from the get parameters ($args_parameter?) in the Nginx config.

I’ve been trying various things out through try_files which I think is
the right area to be looking at. Something like:

try_files /tiles/$args_z_$args_x_$args_y.png proxy_hand_off

Any ideas how I could do this? Anything would be appreciated including
an easier way I haven’t thought of. I naturally want to keep ‘if’
statements down to a minimum, if any at all.

On Fri, Nov 08, 2013 at 05:51:36AM -0600, David Legg wrote:

I’ve been trying various things out through try_files which I think is the right
area to be looking at. Something like:

try_files /tiles/$args_z_$args_x_$args_y.png proxy_hand_off

Any ideas how I could do this?

The arguments to try_files are prefixed with the current $document_root
before being sought as files (with the final argument being magic).

So if you put your to-be-served file in the correct place, and use the
variables correctly, then it should Just Work.

try_files /tiles/${arg_z}${arg_x}${arg_y}.png @fallback;

You might want to wrap that in a “location = /data {}” block.

f

Francis D. [email protected]

On 08/11/2013 22:43, “Francis D.” [email protected] wrote:

The arguments to try_files are prefixed with the current $document_root
before being sought as files (with the final argument being magic).

So if you put your to-be-served file in the correct place, and use the
variables correctly, then it should Just Work.

try_files /tiles/${arg_z}${arg_x}${arg_y}.png @fallback;

You might want to wrap that in a “location = /data {}” block.

Yep, thats exactly what Im looking for and its working fine. I dont
think I had the syntax in the arguments quite correct. Its not just a
question of playing about with the location as to where things will be.