djs*_*dog 5 configuration nginx
我想让 nginx 通过检查该路径/文件是否存在于几个单独的目录中来处理对特定目录的请求。例如,如果我的根目录具有以下内容:
images/siteA/foo.jpg
images/siteB/b/bar.jpg
images/siteC/example.jpg
Run Code Online (Sandbox Code Playgroud)
我想http://example.com/images/foo.jpg返回文件foo.jpg,http://example.com/b/bar.jpg返回文件bar.jpg等等。
我尝试了以下操作,但它只是被锁定在重定向循环中(我不希望它重定向,但实际上将文件提供给该 URL):
location /images/ {
try_files /siteA/images/
/siteB/images/
/siteC/images/ =404;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用捕获组,例如location ~/images/(.*)/并添加$1到 URL 的末尾。我对文档有点困惑,不确定如何使用 nginx 完成此操作。
max*_*-lt 10
作为接受答案的替代方案,您可以使用命名位置(@my_path例如)回退到其他位置,例如:
location / {
root /etc/nginx/static;
try_files $uri @index;
}
location @index {
root /srv/http;
try_files $uri $uri.html /index.html =404;
}
Run Code Online (Sandbox Code Playgroud)
您将需要使用正则表达式 location来捕获 URI 后面的部分/images/。然后使用try_files每个站点前缀来测试一系列 URI。
例如:
location ~ ^/images/(.*)$ {
root /path/to/docroot/images;
try_files /siteA/$1 /siteB/$1 /siteC/$1 =404;
}
Run Code Online (Sandbox Code Playgroud)
您可以root从周围块继承 的值,在这种情况下,try_files适当地调整参数。