我正在使用"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;
    }

最佳答案

在此线程中找到答案
React-router and nginx

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

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

关于reactjs - React Router路由在Nginx create-react-app上不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49251167/

10-16 23:40