【问题标题】:React Router BrowserRouter leads to "404 Not Found - nginx " error when going to subpage directly without through a home-page clickReact Router BrowserRouter在没有通过主页点击直接进入子页面时导致“404 Not Found - nginx”错误
【发布时间】:2018-01-17 19:43:01
【问题描述】:

我正在使用 React Router 为多页网站进行路由。当尝试直接转到子页面https://test0809.herokuapp.com/signin 时,您会收到“404 Not Found -nginx”错误(为了能够看到这个问题,您可能需要在隐身模式下转到此链接,因此没有缓存)。如果您从主页转到所有链接都可以正常工作:test0809.herokuapp.com/。我正在使用 BrowserRouter,并且能够通过将 BrowserRouter 更改为 HashRouter 来消除“404 not found”错误,这会给我的所有 url 一个“#”符号。除了在你的 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

【问题讨论】:

    标签: javascript reactjs nginx react-router linkedin-api


    【解决方案1】:

    问题是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;
      }
    }
    

    【讨论】:

    • 疯狂/相关的旁注,如果您将其与envsubst 一起使用并且忘记转义$ 符号,则会收到一条错误消息,指出[emerg] invalid number of arguments in "try_files" directive。在这种情况下不要忘记逃避美元,你很高兴。我花了 30 分钟才发现我的使用问题。
    【解决方案2】:

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

    server {
       listen       80;
       server_name  localhost;
    
       location / {
           root   /build;
           index  index.html;
           try_files $uri $uri/ /index.html;
       }
    }
    

    【讨论】:

      【解决方案3】:

      在位置添加这个

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

      【讨论】:

        【解决方案4】:

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

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

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

        第 2 步,添加这个 Heroku 构建包:https://github.com/heroku/heroku-buildpack-static.git

        【讨论】:

        • 如果我使用的是 Cloud Foundry 而不是 Heroku 怎么办?另外,这是假设您的根目录中有一个 index.html 吗?我在 root/public/index.html 中有我的顶级 index.html,但它不包含任何路由信息。我的路由由 root/src/app/routeConfig.js 中的 react-router-dom 处理
        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2017-07-15
          • 2015-05-31
          • 1970-01-01
          • 2019-12-10
          • 2021-07-25
          • 2019-09-20
          • 1970-01-01
          • 2020-07-23
          • 2016-10-14
          相关资源
          最近更新 更多