使用路由器时,使用 nginx 响应应用程序不切换组件

我正在尝试使用 nginx 部署我的 React 应用程序。


我遇到的问题是在更改路由时,例如 /about 它实际上并没有更新前端,它只是保留在索引上。


这是我在可用站点中的配置:


server {

  listen 80 default_server;

  server_name server_ip_here;

  root /sites/FrontEnd/React/build;

  index index.html;


  access_log /var/log/nginx/krim.com.access.log;

  error_log /var/log/nginx/krim.com.error.log;


  location / {

      try_files $uri /index.html;

  }

}

这是路由器:


import {Redirect, BrowserRouter as Router,Route,Switch} from 'react-router-dom';


<Router>

      <CookieConsent buttonText="Accept">

      </CookieConsent>


      <Switch>

        <Route exact path="/" component={Home} />

        <Route path="/About" component={Over}/>

        <Route path="/FAQ" component={FAQ}/>

      </Switch>


</Router>

现在,页面索引已加载,但在使用 nginx 时转到 my-ip/About 或 my/FAQ 不会执行任何操作。


现在使用npm start它一切正常,而且使用serve -s build也很好,所以这似乎是我的一些 nginx 配置错误。


任何帮助表示赞赏,干杯。


编辑:更正,本地serve确实有效,但在远程虚拟机上它也不起作用。


哆啦的时光机
浏览 200回答 2
2回答

茅侃侃

我在自己的电脑上构建了这个包,然后将它上传到虚拟机,现在它可以正常工作了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript