Nginx:在最接近的父文件夹中提供index.html

我在单独的文件夹中运行了几个SPA以及一些静态文件。 结构如下:

| |- index.html |- foobar.html |---- SPA1/ | |- index.html | |---- SPA1_1/ | |- index.html |---- SPA2/ |- index.html 

预期的行为是:

  • 所有/SPA1/SPA1_1/foo/barSPA1/SPA1_1/index.html
  • SPAx/index.html服务所有的SPAx/foo/bar
  • /index.html服务/not_exist
  • /foobar.html提供/foobar.html

总之,我希望nginx按顺序尝试以下path:

  • $uri
  • $uri最亲密的父母

有没有什么办法可以达到这个目的而不需要为每个SPA目录指定规则?

您可以通过每次删除目录path来recursion地重写URI,直到findindex.html文件。 这将是一个内部循环,不会发生外部redirect。

例如:

 location / { try_files $uri $uri/index.html @rewrite; } location @rewrite { rewrite ^(.*)/.+ $1/ last; }