I have the following nginx config:
server {
location ~ ^/(\d+)/(\d+)/(\d+)\.png$ {
alias /mount/cache/c/$1/$2/$3.png;
try_files $uri @proxy;
add_header x-source file;
}
location @proxy {
proxy_pass http://127.0.0.1:28000;
add_header x-source proxy;
}
}
What I'm trying to achieve is to try to load the files from disk, and if they are not found only then ask the upstream for it.
My problem is that try_files
doesn't seem to work, it always requests @proxy (x-source header is always proxy). If I comment out try_files
then it loads the files from the disk correctly, but then the proxy part doesn't work.
In this example you don't need to use
alias
at all.If you need an
alias
, then don't usetry_files
. Use, for example,error_page
.The issue becomes from your
alias
directive.Suppose a resource
http://example.com/1/2/3.png
is requested.The
alias
directive means that nginx will look up a file at/mount/cache/c/1/2/3.png/1/2/3/.png
. This is becausealias
sets the base path where files are located, and then the$uri
is appended to the get the final resource.This should work with this config:
provided that your files are located in
/mount/cache/c
directory.