如何为nginx配置多个root?

yum*_*yum 3 nginx

location ~ {
            root /var/www/static1/;
            root /var/www/static2/;
        }
Run Code Online (Sandbox Code Playgroud)

我尝试了上述方法,以便在/name.gif请求时,如果/var/www/static1/name.gif存在,则按原样使用它;但是如果没有这样的文件,请尝试查看是否有/var/www/static2/name.gif,是否可以通过这种方式配置 nginx?

kol*_*ack 9

假设您使用的是 nginx 0.7.24(?) 或更高版本,您可以使用 try_files 完成此操作:

location / {
    root /var/www;
    try_files /static1$uri /static2$uri =404;
}
Run Code Online (Sandbox Code Playgroud)

通常,将 root 放在 location / 中是不正确的,但在这种特定情况下,我认为您不想将 /var/www 设置为您的一般 root,以防您最终添加其他位置。