具有spa和子目录根目录的nginx配置

具有spa和子目录根目录的nginx配置

本文介绍了具有spa和子目录根目录的nginx配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎总是对nginx配置有问题.我的SPA位于/mnt/q/app(启用了推送状态),前端根目录位于客户端/公共.一切都应映射到index.html,在此应用程序将选择路线并决定要执行的操作.

I always seem to have problems with nginx configurations. My SPA is located at /mnt/q/app (pushstate is enabled) and the frontend root is located at client/public. Everything should be mapped to index.html, where the app picks up the route and decides what to do.

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

我想我现在已经没有选择了.不管我做什么,我都只能从nginx处获得404,我认为配置很简单,不知道出了什么问题.

I think I ran out of options by now. No matter what I do, I just get a 404 back from nginx, I think the configuration is simple enought and have no clue what's wrong.

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;
    }
}

感谢您的帮助.

推荐答案

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

If nginx views the file system from the root, then the root should be set to /mnt/q/app/client/public, and not either of the two values you are using.

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

The last element of the try_files directive can be a default action (e.g. /index.html), a named location or a response code. You have a named location in the penultimate element - which will be ignored.

您的命名位置应该可以使用,但是没有必要,因为 try_files 能够更简单地实现它.有关更多信息,请参见此文档.

Your named location should work, but is unnecessary, as try_files is capable of implementing it more simply. See this document for more.

例如:

root /mnt/q/app;

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

location /api {
}

location /auth {
}

$ uri/元素将在目录中添加尾随的/,因此 index 指令可以工作-您不必如果您不需要它,请添加它.

The $uri/ element will add a trailing / to directories, so that the index directive can work - you do not have to add it if you do not need it.

这篇关于具有spa和子目录根目录的nginx配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:10