4

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.

3
  • Does /data/example.com contain /sites/default/files/? If so: 1.) Remove the location location /sites/default/files/ { [...] } directive. 2.) Add $uri to the try_files directive in location / { [...] }.
    – gxx
    Commented Jun 29, 2016 at 23:53
  • Yes /data/example.com/sites/default/files exists but I don't want /data/example.com/foobar to be served directly only /data/example.com/sites/default/files .
    – chx
    Commented Jun 30, 2016 at 0:59
  • @chx you could use internal; directive for that purpose.
    – Unicornist
    Commented Sep 2, 2018 at 5:34

1 Answer 1

8

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;
}
1
  • > You could put your proxy config to separate file and include it. - I really like this one and will use it.
    – chx
    Commented Jul 1, 2016 at 11:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .