Return file with nginx if exists, pass to proxy else

arts777 asked:

The task is to get argument with filename from query params, check if file exists, and:

  • If exists, return this file
  • Else, pass the request to Django backend

I’ve managed to get the param and check if file exists, but I doesn’t know what to do next:

location /media/ {
                set     $file "/static/$arg_key";

                if (-f $file) {
                        // Return the file
                } else {
                        // include proxy_params;
                        // proxy_pass      http://unix:/opt/django/gunicorn.sock;

My answer:

This is what try_files is for.

For example:

location /media/ {
    try_files /static/$arg_key @django;

location @django {

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.