I uploaded react.js application to server. I'm using nginx server. Application is working fine. But when I go to other page & refresh, site is not working. It's showing 404 Not found error.
How can I solve this.
Thank You.
I uploaded react.js application to server. I'm using nginx server. Application is working fine. But when I go to other page & refresh, site is not working. It's showing 404 Not found error.
How can I solve this.
Thank You.
When your react.js
app loads, the routes are handled on the frontend by the react-router
. Say for example you are at http://a.com
. Then on the page you navigate to http://a.com/b
. This route change is handled in the browser itself. Now when you refresh or open the url http://a.com/b
in the a new tab, the request goes to your nginx
where the particular route does not exist and hence you get 404.
To avoid this, you need to load the root file(usually index.html) for all non matching routes so that nginx
sends the file and the route is then handled by your react app on the browser. To do this you have to make the below change in your nginx.conf
or sites-enabled
appropiately
location / {
try_files $uri /index.html;
}
This tells nginx
to look for the specified $uri
, if it cannot find one then it send index.html
back to the browser.