Bear with me, this is a long one.
In the beginning I created a Nginx server to server static files for my company, and it worked great with the following config snippet for serving the files:
server {
# bunch of ssl stuff
root /path/to/files;
location / {
include cors_support;
sendfile on;
sendfile_max_chunk 1m;
try_files $uri =404;
}
Simple, clean and functional. Until I thought I would add another function to this server: storing our application builds and serving them to our QA team. Since the application files can be quite big, I added another storage volume just for the builds and thought I could get away with this with a simple change to the Nginx config as follows (only relevant portions shown):
server {
# mostly the same
root /new/path/to/builds;
location / {
#same as before
try_files $uri @cdn_files;
}
location @cdn_files {
#some cors and send_file settings
root /path/to/files;
try_files $uri =404;
}
}
What I expected to happen according to documentation:
- request comes in for a file
- checks the first block for the file
- file found? yes=sends file no=redirect to named location
- named location -> file found? yes=sends file no=404 page
What actually happens:
- request comes in for a file
- checks the first block for the file
- file found? yes=sends file no=404 page
I have tried several different attempts at fixing this, including some insane "hail mary" attempts.
This produces the same result (404 page):
# ... irrelevant code
root /;
location / {
# ... more irrelevant code
try_files /new/path/to/builds/$uri /path/to/files/$uri =404;
}
This produces the same result (404 page):
# ...
root /new/path/to/builds;
location / {
# ...
try_files $uri @cdn_files;
}
location @cdn_files {
# ...
root /path/to/files;
try_files $uri =403; #note that the page delivered is 404 and NOT 403
}
This produces a 404 as well:
# I removed the "root" directive from the server block for this test
location / {
# ...
root /new/path/to/builds;
try_files $uri ../realitve/path/to/files/$uri =404;
}
A couple of other things to note:
- The error logs only show 1 attempt at locating the file at the first parameter of the
try_files
directive.
- If the file exists in the first parameter of the
try_files
directive the file is delivered
- I don't think its relative but just in case: OS is Ubuntu 16.04
Edit: