带spa和子目录root的nginx配置

mar*_*kpw 6 nginx single-page-application aurelia

我似乎总是遇到nginx配置问题.我的SPA位于/ mnt/q/app(启用了pushstate),前端根位于客户端/公共端.应该将所有内容映射到index.html,应用程序会选择路由并决定要执行的操作.

索引的完整路径是/mnt/q/app/client/public/index.html.

我想我现在用完了选项.无论我做什么,我只是从nginx获得404,我认为配置很简单,并且不知道什么是错的.

server {
    listen 80;
    server_name app.dev;

    root /mnt/q/app;

    location / {
      root /client/public;
      try_files $uri @rewrites =404;
    }

    location @rewrites {
       rewrite ^(.+)$ /index.html last;
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

Ric*_*ith 11

如果nginx从根目录查看文件系统,root则应将其设置为/mnt/q/app/client/public,而不是您正在使用的两个值中的任何一个.

try_files指令的最后一个元素可以是默认操作(例如/index.html),命名位置或响应代码.您在倒数第二个元素中有一个命名位置 - 将被忽略.

您的指定位置应该有效,但是没有必要,因为try_files它能够更简单地实现它.有关更多信息,请参阅此文

例如:

root /mnt/q/app;

location / {
    root /mnt/q/app/client/public;
    try_files $uri $uri/ /index.html;
}

location /api {
}

location /auth {
}
Run Code Online (Sandbox Code Playgroud)

$uri/元素将向/目录添加一个尾随,以便该index指令可以工作 - 如果您不需要它,则不必添加它.