我正在使用React路由为多页网站路由.当试图直接转到子页面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包含#.

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应用程序启动了这个项目.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;
  }
}

Reactjs相关问答推荐

Redux toolet TypHelp:在使用redux thunk获取数据时无法读取未定义(读取类型)的属性

在Redux工具包查询中,是否可以将标记应用到API切片内的所有端点?

FireBase Reaction Native-在呈现视图之前等待响应

Next.js Next-Auth登录函数重定向到http://localhost:3000/api/auth/error

当useEffect和onClick处理程序都调用useCallback函数时,有没有办法对useEffect产生额外的影响?

根据用户 Select 的注册类型更改表单字段

为什么React UseEffect挂钩在页面重新加载时运行

useEffect firebase 清理功能如何工作?

UseEffect 似乎不适用于页面的首次渲染

我无法通过 useContext 显示值

在添加了useplacesautocomplete后,在构建React/Next.js项目时,NPM抛出类型期望的错误

React设置上下文并进行导航

未捕获的运行时错误:对象作为 React 子项无效

ReactJS 动态禁用按钮

如何在 ChartJS 中操作 Y 轴

如何在 React 中使用 debounce hooks

动态添加或删除文本输入字段并将值设置为 React JS 中主要状态的数组

React Native PermissionsAndroid 不包括 READ_MEDIA_IMAGES.如何解决这个问题?

如何将值从下拉列表传递到 select 上的输入?响应式JS

为什么此代码中的 useState 不更新(文本更新但 id 不更新)?