【问题标题】:Create react app breaks after deployment on refresh 404 with Nginx在使用 Nginx 刷新 404 部署后创建反应应用程序中断
【发布时间】:2020-03-19 07:05:39
【问题描述】:

我正在使用 create-react-app,它在我的本地运行良好,完全没有问题。当我将它部署在 IBM Cloud 上时,登录后,当我刷新页面时,它给出 404 not found 错误它之前工作正常,不知道发生了什么。

我看到了很多相关的问题,我试图解决但没有工作的事情如下

1. 创建了一个static.json

{
  "root": "build/",
  "routes": {
    "/**": "index.html"
  }
}

2。我有这个设置

devServer: {
    historyApiFallback: true,
    contentBase: './',
    hot: true
  },

3.我尝试在路由器周围添加,没有用

import React, { Component } from 'react';
import styled from 'styled-components';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

    class App extends Component {
      render() {
        return (
          <>
            <Router>
              <Switch>
                <Route exact path="/" component={SignUp} />
                <Route path="/some" component={Some} />
              </Switch>
            </Router>
            </>
        );
      }
    }

    export default App;

4.我尝试添加以下内容

import { HashRouter } from 'react-router-dom'

<HashRouter>
  <App/>
</HashRouter>

这些似乎都不适合我。任何建议

【问题讨论】:

    标签: javascript reactjs nginx ibm-cloud create-react-app


    【解决方案1】:

    当将 build/ 输出作为静态站点提供时,您必须将 nginx 配置为始终提供 index.html (否则,它将尝试将 url 路径与不存在的静态文件夹/文件匹配)。有关此行为的更多信息,请参阅 create-react-app docs

    如果你使用 nginx docker 镜像来服务你的站点,你需要设置如下default.conf:

    server {
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
    
        location ~ ^/$ {
            rewrite  ^.*$  /index.html  last;
        }
        listen       80;
        server_name  localhost;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    

    注意rewrite ^.*$ /index.html 部分,它修改任何传入呼叫并为index.html 提供服务。

    您的最小 Dockerfile 将如下所示:

    FROM nginx
    COPY default.conf /etc/nginx/conf.d/default.conf
    COPY build/ /usr/share/nginx/html/
    

    【讨论】:

      【解决方案2】:

      我不相信这样做:

      <HashRouter> 
        <Router>
          <Switch>
          </Switch>
        </Router>
      </HashRouter>
      

      我认为您需要构建应用程序并运行它。我看到一个常见错误:

      react-scripts start
      /bin/sh: 1: react-scripts: not found
      

      希望能帮到你!

      【讨论】:

        【解决方案3】:

        我终于明白了。感谢 Sebastián Sethson 的回答,这导致了正确的道路。

        我将路由更新到

        <HashRouter basename="/"> 
            <Switch>
              <Route exact path="/" component={SignUp} />
              <Route path="/some" component={Some} />
            </Switch>
        </HashRouter>
        

        【讨论】:

          猜你喜欢
          • 2018-09-20
          • 2015-08-02
          • 1970-01-01
          • 2023-03-30
          • 1970-01-01
          • 1970-01-01
          • 2014-01-24
          • 2018-11-30
          • 1970-01-01
          相关资源
          最近更新 更多