【问题标题】:Support direct linking to react-router paths with nginx on local server支持使用本地服务器上的 nginx 直接链接到 react-router 路径
【发布时间】:2017-06-30 02:07:06
【问题描述】:

我正在尝试设置本地 react 和 rails 环境。我的 React 应用程序目前托管在 http://localhost:8080,我的 rails 应用程序托管在 http://localhost:3000。我希望这两个都从同一个网址提供,例如http://localhost:9999

基本原理如下:

http {
  server {
    listen 9999;

    location / {
      proxy_pass http://localhost:8080/;
    }

    location /api {
      proxy_pass http://localhost:3000/;
    }
  }
}

问题是在地址栏中粘贴/ 以外的路由不起作用——这是一个常见问题。

因此,一个常见的答案是将location / 规则更改为如下所示:

location / {
  try_files $uri $uri/ /client/index.html
}

但是,这给了我错误:

在内部重定向到“/client/index.html”时重写或内部重定向循环

这是有道理的,但我不知道如何绕过它。

我的目录结构:

root/
 - app/ (rails app)
 - client/ (react app, currently served by webpack-dev-server)
   - index.html

【问题讨论】:

    标签: ruby-on-rails reactjs nginx react-router


    【解决方案1】:

    这是一个很老的问题,但我遇到了同样的问题,最后设法解决了。

    在我的例子中,以下配置完成了这项工作:

    location / {
        try_files $uri /index.html;
        root /usr/share/nginx/html;
    }
    

    只需将root 指向您的 WWW 文件所在的位置。

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 1970-01-01
      • 2016-08-20
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 2021-12-29
      相关资源
      最近更新 更多