Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router routes not working on nginx create-react-app

I'm using "react-router-dom": "^4.2.2".

If I test on localhost:3000/second it works perfectly.

When I upload this on ubuntu server with nginx and I try www.website.com, it works . When I try to use www.website.com/second it gives me 404 not found. I'm using 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 Here's the configuration file from the server

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;
    }
like image 803
sander Avatar asked Mar 13 '18 08:03

sander


People also ask

Does create React app React to Router?

To get started with React Router in a web app, you'll need a React web app. If you need to create one, we recommend you try Create React App. It's a popular tool that works really well with React Router. First, install create-react-app and make a new project with it.

How do I add React Router to React app?

To install React Router, all you have to do is run npm install react-router-dom@6 in your project terminal and then wait for the installation to complete. If you are using yarn then use this command: yarn add react-router-dom@6 .


2 Answers

The answer is found in this thread React-router and nginx

What I had to do was modify default configuration file in /etc/nginx/sites-available/default to:

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri /index.html;
    }
like image 191
sander Avatar answered Oct 17 '22 17:10

sander


If you wish to do this in a Dockerfile and run your React app, using nginx please see the below answer.

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.

like image 28
Keet Sugathadasa Avatar answered Oct 17 '22 16:10

Keet Sugathadasa