【问题标题】:Serve multiple React apps from the same server with Nginx使用 Nginx 从同一服务器提供多个 React 应用程序
【发布时间】:2019-01-13 00:23:03
【问题描述】:

我在 Nginx 的同一个服务器块中为多个 react 应用程序提供服务。我将这些路由从 nginx 转发到每个 react 应用程序的 index.html,我已经在每个位置添加了一个 try_files:

我的 nginx 配置:

server {
  listen 80;
  location /nest/ {
        alias /Users/harikrishnan/turbolab/nest-ui/build;
        try_files $uri /index.html;
  }
  location  /nest/dataviewer/ {
     alias /Users/harikrishnan/turbolab/dataviewer/dataviewer/build;
     try_files $uri /index.html;
}
}

问题是当我浏览到/nest/dataviewer 时,它会重定向到/nest/。这显然不是预期的,因为我希望每个位置都转发到适当项目的index.html。

【问题讨论】:

  • 我觉得用两个通配符~*不是个好办法,另外这个问题应该在“ServerFault”上问(serverfault.com)

标签: reactjs nginx nginx-location


【解决方案1】:

正则表达式 (^nest/dataviewer) 与以 /nest/dataviewer 开头的 URI 不匹配。您缺少领先的/。

location ~* { 语句缺少任何正则表达式,我很惊讶nginx 接受它。

在这种情况下,您应该使用前缀location,这样更有效。详情请见this document。

例如:

location /nest/dataviewer { ... }
location / { ... }

【讨论】:

  • 感谢您的回复。现在第二个应用程序已加载,但组件未加载。
  • 只加载 index.html 不加载项目文件
猜你喜欢
  • 2018-06-16
  • 2019-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2019-12-19
  • 2020-09-27
  • 2015-08-15
相关资源
最近更新 更多