nginx - 提供不同路线的SPA

时间:2018-04-20 08:30:49

标签: nginx

我在/ usr / app / build中有一个包。它有一个index.html。

我想:

  1. 请求/ build从/ usr / app / build
  2. 提供
  3. /usr/app/build/index.html
  4. 要回答的任何其他请求

    就是这样!

    以下不起作用(给出404)。也不会将try_files参数替换为/build/index.html,也不会为/index.html提供明确的位置,并且try_files参数为/index.html

    location /build {
        alias /usr/app/build;
    }
    
    location / {
        try_files /usr/app/build/index.html =404;
    }
    

    我很困惑。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

提供文件的任何位置都必须包含rootalias,或者从周围的块继承值。在您的情况下,rootalias效率更高(有关详细信息,请参阅this document)。

try_files语句的文件元素附加到root值以生成返回文件的路径名。有关详细信息,请参阅this document

例如:

root /usr/app;
location /build {}
location / {
    try_files /build/index.html =404;
}

如果=404始终存在,则/usr/app/build/index.html基本上是多余的。将默认操作放在try_files语句的末尾更为常见,例如:

root /usr/app;
location / {
    try_files $uri $uri/ /build/index.html;
}

但上述内容并不符合"任何其他要求的确切要求"。

相关问题