try_files with a single @location?

I started from https://gist.github.com/mattd/1006398 and now I have this:

    root /data/example.com;
    location /sites/default/files/ {
            try_files $uri @proxy;
    }
    location / {
            try_files @proxy;
    }
    location @proxy {
            proxy_pass http://127.0.0.1:8088;
    }

Of course, this is not valid syntax. How can I make this work? Basically, I want sites/default/files to serve the file directly if it exists and use @proxy otherwise. Other directories should go to @proxy immediately. I do not want to copy-paste the @proxy configuration because a lot more will be added there (caching). I will also have other directories that should be served directly for example .well-known.


There are several ways to solve the problem:

1

You could put your proxy config to separate file and include it.

proxy_conf:

proxy_pass proxy_pass http://127.0.0.1:8088;
proxy_set_header Host $host;
# etc.....

and your config:

location /sites/default/files/ {
   try_files $uri @proxy;
}
location / {
    include proxy_conf;
}
location @proxy {
    include proxy_conf;
}

2

Use error_page directive:

location / {
    error_page 418 = @proxy;
    return 418;
}

3

Use fake non-existent path as a first argument to try_files:

location / {
    try_files /NONEXISTENTFILE @proxy;
}