Nginx 仅遵循 `location /` 规则

Nginx only following `location /` rule

我是 nginx 新手,想创建两种不同的规则:一种指定 index.html 作为响应,另一种通过 uwsgi 将请求转发到 Flask 服务器。如果我只包含一个带有 location / 的位置块,我会得到正确的响应,具体取决于我在块中放入的内容(index.html 或来自烧瓶的预期响应),但如果我同时放入两个规则location / 块始终按预期工作,而 location /test 块以 404 响应。

我希望以下服务器块为 //test return 相同的 index.html 文件,但只有第一个按预期工作。关于如何解决这个问题有什么想法吗?

server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/src;

        location / {
                index index.html;
        }

        location /test {
                index index.html;
        }
}

Module ngx_http_index_module

Defines files that will be used as an index. The file name can contain variables. Files are checked in the specified order. The last element of the list can be a file with an absolute path. Example:

重要的部分是:"Defines files",而不是路径。这意味着,目录 test.

中必须有一个名为 index.html 的文件

如果您真的想从根目录提供 index.html,您可以使用绝对路径作为最后一个(仅在您的情况下)文件

location /test {
    index /index.html;
}