Nginx多个根位置

时间:2012-11-17 19:36:06

标签: nginx location root

我有一个像这样定义的位置。

location ~ \.(jpg|jpeg|png|gif|swf|flv|mp4|mov|avi|wmv|m4v|mkv|ico|css|js|txt|html|htm)$ {
    root /path/to/static/files/;
}

如果我请求/events.js中的/path/to/static/files/,则可以正常使用。

但是,如果我请求/func.js中的/other/path/to/static/files/,则无法找到它。

所以我需要的是两个可以找到静态文件的位置。这两个请求之间没有任何其他差异,它们在同一个应用程序中是相同的,唯一的区别是文件的物理位置。另外,我不知道文件的名称是完全随机的。

解决方案

location ~ \.(jpg|jpeg|png|gif|swf|flv|mp4|mov|avi|wmv|m4v|mkv|ico|css|js|txt|html|htm)$ {
    root /;
    try_files /path/to/static/files/$uri /other/path/to/static/files/$uri /;
}

1 个答案:

答案 0 :(得分:0)

尝试以下内容:

location ~ \.(jpg|jpeg|png|gif|swf|flv|mp4|mov|avi|wmv|m4v|mkv|ico|css|js|txt|html|htm)$ {
  root /;
  try_files /path/to/static/files/$uri /other/path/to/static/files/$uri;
}

请参阅http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files了解其工作原理