Forum: NGINX File Checking Rewriting and Handing off to Back End

91542f04170af4295876ae34aa026ad9?d=identicon&s=25 David Legg (Guest)
on 2013-11-08 12:52
(Received via mailing list)
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.
36a8284995fa0fb82e6aa2bede32adac?d=identicon&s=25 Francis Daly (Guest)
on 2013-11-08 23:44
(Received via mailing list)
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 Daly        francis@daoine.org
91542f04170af4295876ae34aa026ad9?d=identicon&s=25 David Legg (Guest)
on 2013-11-11 00:09
(Received via mailing list)
On 08/11/2013 22:43, "Francis Daly" <francis@daoine.org> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.