【问题标题】:React Router routes not working on nginx create-react-appReact Router 路由在 nginx create-react-app 上不起作用
【发布时间】:2018-08-21 09:56:00
【问题描述】:

我正在使用"react-router-dom": "^4.2.2"

如果我在 localhost:3000/second 上进行测试,它会完美运行。

当我使用 nginx 在 ubuntu 服务器上上传它并尝试 www.website.com 时,它可以工作。当我尝试使用www.website.com/second 时,它给了我404 not found。我正在使用create-react-app

app.js

class TestRoutes extends React.Component{
    constructor(props){
        super(props);
    }
    render(){
        return(<React.Fragment>
            <BrowserRouter>
                <Switch>
                    <Route exact path='/' component={MainPage}/>
                    <Route path='/second' component={SecondPage}/>
                    <Route path='/third' component={ThirdPage}/>
                </Switch>
            </BrowserRouter>
                </React.Fragment>);
    }
}

ReactDOM.render(<TestRoutes/>, document.getElementById("root"));

/etc/nginx/sites-available/default 这是来自服务器的配置文件

server {
        listen 443 ssl;

    root /var/www/reactcamera/build;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name website.com www.website.com;
    ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem; # 
    managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/website.com/privkey.pem; # 
    managed by Certbot


    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

【问题讨论】:

  • 这里有很多可能出错的地方。你有重定向到 create-react-app 在 yarn build 上创建的 dist 目录吗?你建了吗?等等……
  • 是的,我从 git 克隆了它并做了npm installnpm run build
  • 你能显示你的 nginx 配置文件吗
  • nginx.conf?因此,当您转到 URL 时,它会指向某些东西吗?
  • 在原帖中添加了配置文件

标签: reactjs nginx react-router create-react-app


【解决方案1】:

答案在这个帖子里找到 React-router and nginx

我要做的是将/etc/nginx/sites-available/default中的default配置文件修改为:

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri /index.html;
    }

【讨论】:

    【解决方案2】:

    如果您希望在Dockerfile 中执行此操作并运行您的React 应用程序,请使用nginx,请参阅以下答案。

    https://stackoverflow.com/a/61753597/4388776

    这解决了 React Router 问题,您允许 nginx 将来自不同端点的流量直接路由到您的 React 应用程序。

    【讨论】:

      【解决方案3】:

      在实时服务器中适合我的解决方案:

      server {
              listen 80;
              listen [::]:80;
      
              root /var/www/example.com;
              index index.html index.htm index.nginx-debian.html;
      
              server_name example.com www.example.com;
      
              location / {
                      try_files $uri $uri/ /index.html;
              }
      }
      

      【讨论】:

        【解决方案4】:

        你需要通过 /etc/nginx/sites-available/ 来配置 nginx。

        会有一个模板默认文件,所以如果您想保留它,请复制它。完成此操作后,使用任何文本编辑器并将 /etc/nginx/sites-available/default 更改为以下内容:

        server {
           listen 80 default_server;
           root /var/www/[Your repo name]/build;
           server_name [your.domain.com] [your other domain if you want to];
           index index.html index.html;
           location / {
           }
        }
        

        【讨论】:

        • 感谢您的评论。我在原帖中添加了/etc/nginx/sites-available/,我的配置有问题吗?我是否需要以某种方式告诉服务器每个路径,例如www.website.com/second
        猜你喜欢
        • 2017-02-08
        • 2019-12-11
        • 2018-09-30
        • 2017-09-13
        • 1970-01-01
        • 2018-11-08
        • 2021-05-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多