Nginx try_files or else continue matching against locations?
Posted
by
Yang
on Server Fault
See other posts from Server Fault
or by Yang
Published on 2012-04-14T23:53:58Z
Indexed on
2012/04/15
5:35 UTC
Read the original article
Hit count: 457
nginx
I'm wondering whether this is possible with Nginx: I just added a directory with a bunch of HTML files (foo.html, bar.html) that I'd like to serve with /foo, /bar, etc. If the URL doesn't match up with a file name I'd like to fall back to whatever the next best matching location
would be.
So I have:
# This block is newly added.
location ~ ^/([^/]+)$ {
default_type text/html;
alias /blah/$1.html;
}
# Our long list of existing subsystems below....
location /subscribe {
proxy_pass http://127.0.0.1:5000;
}
location /upload {
proxy_pass http://127.0.0.1:8090;
proxy_read_timeout 99999;
}
location ~ /(data|garbage|blargh).* {
proxy_pass http://127.0.0.1:8090;
proxy_read_timeout 99999;
auth_basic text;
auth_basic_user_file /etc/nginx/htpasswd;
}
....
The problem is that the first regex now eats up the URLs that would've gone to other location
s, as per the documented behavior of location
.
One approach is to maintain the full explicit list of files in the first location
block, but this list is quite large and is always changing. Is there a way to check to see if the file exists first, and if not, then continue with what would've been the next-best location
match?
I took stabs using try_files
(including using a @fallback and nesting location
s in there) but I don't think it's capable of doing this. However I thought I'd ask here in case I'm missing something. (Or maybe there's another better approach altogether.)
© Server Fault or respective owner