简体   繁体   English

React Router 路由不适用于 nginx create-react-app

[英]React Router routes not working on nginx create-react-app

I'm using "react-router-dom": "^4.2.2" .我正在使用"react-router-dom": "^4.2.2"

If I test on localhost:3000/second it works perfectly.如果我在localhost:3000/second上测试,它会完美运行。

When I upload this on ubuntu server with nginx and I try www.website.com , it works.当我使用 nginx 在 ubuntu 服务器上上传它并尝试www.website.com时,它可以工作。 When I try to use www.website.com/second it gives me 404 not found .当我尝试使用www.website.com/second时,它给我404 not found I'm using create-react-app .我正在使用create-react-app

app.js应用程序.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 Here's the configuration file from the server /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;
    }

The answer is found in this thread React-router and nginx答案在这个线程React-router and nginx中找到

What I had to do was modify default configuration file in /etc/nginx/sites-available/default to:我要做的是将/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;
    }

If you wish to do this in a Dockerfile and run your React app, using nginx please see the below answer.如果您希望在Dockerfile中执行此操作并使用nginx运行您的React应用程序,请参阅以下答案。

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

This fixes the React Router issues where you allow nginx to route traffic coming on different endpoints, directly to you React Application.这修复了React Router问题,您允许nginx将来自不同端点的流量直接路由到您的 React 应用程序。

The solution that works for me in the live server:在实时服务器中对我有用的解决方案:

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;
        }
}

You need to configure nginx by going /etc/nginx/sites-available/.您需要通过 /etc/nginx/sites-available/ 配置 nginx。

There will be a template default file so if you would like to keep it, copy it.将有一个模板默认文件,因此如果您想保留它,请复制它。 After you do that, use any text editor and change /etc/nginx/sites-available/default to following:完成后,使用任何文本编辑器并将 /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 / {
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nginx 和 Create React App (with React Router) 完整路由不起作用 - Nginx and Create React App (with React Router) full routes not working create-react-app 和 react-router 路由在 S3 中无法正常工作 - create-react-app and react-router routes not working correctly in S3 域不适用于 create-react-app nginx - Domain not working on create-react-app nginx React 路由在 facebook 的 create-react-app 构建中不起作用 - React routes are not working in facebook's create-react-app build Create-React-App/React Router 无法通过渲染进行部署 - Create-React-App/React Router Not Working On Deployment via Render 弹出的create-react-app的快速路由 - Express Routes for ejected create-react-app 如何在create-react-app上使用&#39;/&#39;阻止路由 - How to block routes with '/' on create-react-app create-react-app无法正常工作? - create-react-app is not working? 在Github Pages上更改为自定义域会导致带有react-router的create-react-app停止工作 - Changing to custom domain on Github Pages causes create-react-app with react-router to stop working 如何编写在项目创建的 create-react-app 的单页反应路由器上工作的 JS - How to write JS which working on single page of react router at project created create-react-app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM