# File Checking Rewriting and Handing off to Back End

**URL:** <https://www.ruby-forum.com/t/file-checking-rewriting-and-handing-off-to-back-end/237367>\
**Category:** NGINX\
**Created:** [November 8, 2013, 12:52pm UTC](https://www.ruby-forum.com/t/file-checking-rewriting-and-handing-off-to-back-end/237367 "2013-11-08T12:52:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [November 8, 2013, 12:52pm UTC](https://www.ruby-forum.com/t/file-checking-rewriting-and-handing-off-to-back-end/237367/1 "2013-11-08T12:52:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![David\_Legg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@David\_Legg](https://www.ruby-forum.com/u/David_Legg)\
**Post date:** [November 8, 2013, 11:44pm UTC](https://www.ruby-forum.com/t/file-checking-rewriting-and-handing-off-to-back-end/237367/2 "2013-11-08T23:44:29Z")

</div>

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. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![David\_Legg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@David\_Legg](https://www.ruby-forum.com/u/David_Legg)\
**Post date:** [November 11, 2013, 12:09am UTC](https://www.ruby-forum.com/t/file-checking-rewriting-and-handing-off-to-back-end/237367/3 "2013-11-11T00:09:53Z")

</div>

On 08/11/2013 22:43, “Francis D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
