Nginx 检查多个根

ekh*_*sky 1 nginx

我的nginx配置中有一个位置:

   location ~ \.(js|jpg|png|css|gif|ico|svg|jpeg)$ {
            root /some/basic/root;
       }
Run Code Online (Sandbox Code Playgroud)

它适用于一个目录中的静态文件/some/basic/root。但我还有一些其他静态文件,例如cssjs在其他目录中:

root /completely/other/root;

root /completely/other/root/dir1;

root /completely/other/root/dir2;

如何检查nginx其他目录?

Ric*_*ith 5

如果有共同的父目录,可以使用try_files依次测试各个子目录:

location ~ \.(js|css)$ {
    root /common/parent;
    try_files /basic/root$uri /other/root$uri /other/root/dir1$uri /other/root/dir2$uri =404;
}
Run Code Online (Sandbox Code Playgroud)

如果公共父目录不切实际,您可以级联location块,再次使用try_files

location ~ \.(js|css)$ {
    root /some/basic/root;
    try_files $uri @other;
}
location @other {
    root /completely/other/root;
    try_files $uri /dir1$uri /dir2$uri =404;
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅此文档