React Router BrowserRouter 在不通过主页点击直接转到子页面时导致“404 Not Found - nginx”错误

React Router BrowserRouter leads to "404 Not Found - nginx " error when going to subpage directly without through a home-page click

我正在使用 React Router 为多页面网站路由。当试图直接进入子页面时 https://test0809.herokuapp.com/signin 你会得到一个“404 Not Found -nginx”错误(为了能够看到这个问题你可能需要在隐身模式下进入这个 link所以没有缓存)。如果您从主页 test0809.herokuapp.com/ 进入,所有 link 都可以正常工作。我正在使用 BrowserRouter,并且能够通过将 BrowserRouter 更改为 HashRouter 来消除“404 not found”错误,这会为我的所有网址提供一个“#”符号。除了在你的 url 中有一个“#”的所有问题之外,最大的问题是我需要在我的网站中实现 LinkedIn Auth,而 LinkedIn OAuth 2.0 不允许重定向 URL 包含#。 LinedIn OAuth 2.0 error screen grab

import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
import LinkedIn from 'react-linkedin-login'
const Home = () => <div><h2>Home</h2></div>
const About = () => <div><h2>About</h2></div>
class Signin extends Component {
  callbackLinkedIn = code => {
    console.log(1, code)
  }
  render() {
      return (
          <div>
              <h2>Signin</h2>
              <LinkedIn
                  clientId="clientID"
                  callback={this.callbackLinkedIn}
              >
          </div>
      )
  }
}
const BasicExample = () =>
  <Router>
    <div>
      <ul>
         <li>
           <Link to="/">Home</Link>
         </li>
         <li>
           <Link to="/about">About</Link>
         </li>
         <li>
           <Link to="/signin">Signin</Link>
         </li>
      </ul>
  <hr />

      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/signin" component={Signin} />
    </div>
  </Router>
export default BasicExample

有什么解决方法的建议吗?

背景:我用create-react-app开始这个项目。 GitHub 回购:/debelopumento/test0809

问题是 nginx 不知道如何处理 /signin。您需要更改您的 nginx 配置(通常在 /etc/nginx/conf.d/ 中)以服务您的 index.html 而不管路由。这是一个可能有帮助的示例 nginx 配置:

server {
  listen 80 default_server;
  server_name /var/www/example.com;

  root /var/www/example.com;
  index index.html index.htm;      

  location ~* \.(?:manifest|appcache|html?|xml|json)$ {
    expires -1;
    # access_log logs/static.log; # I don't usually include a static log
  }

  location ~* \.(?:css|js)$ {
    try_files $uri =404;
    expires 1y;
    access_log off;
    add_header Cache-Control "public";
  }

  # Any route containing a file extension (e.g. /devicesfile.js)
  location ~ ^.+\..+$ {
    try_files $uri =404;
  }

  # Any route that doesn't have a file extension (e.g. /devices)
  location / {
    try_files $uri $uri/ /index.html;
  }
}

你可以看到我的项目: https://github.com/hongnguyenhuu96/honnh96 您应该将所有代码放在您的 create-react-app 文件夹中以 react-ui 文件夹并保持其他代码不变。部署到 heroku 之后,记得将部署环境配置到 nodejs。它会起作用的,祝你好运! 它还部署在 heroku 上:http://hongnh.herokuapp.com/

两步解决方案(第 1 步在 React-Router README https://github.com/mars/create-react-app-buildpack#routing-clean-urls 中提到):

第一步,添加一个static.json文件到根目录:

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

第 2 步,添加此 Heroku buildpack:https://github.com/heroku/heroku-buildpack-static.git

只需将 try_files $uri $uri/ /index.html; 添加到 NGINX 配置文件中的 location / 块,如下所示:

server {
   listen       80;
   server_name  localhost;

   location / {
       root   /build;
       index  index.html;
       try_files $uri $uri/ /index.html;
   }
}

在现场添加这个

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