Configuring NGINX to work with React Router
· Reading time: ~1 minute(s) (168 words) programming javascript nginx techWhen using the BrowserRouter
that’s provided by React Router, you’ll find that if you’re navigating within the site to the various URLs, they will work. However if you bookmarked one of those URLs, or refreshed the browser on a URL, it will return a 404.
If you’re using Webpack’s devserver, then you can get around it with a setting in the webpack.config.js file:
|
|
This setting makes webpack aware of the historyApi that is used by React Router and essentially just redirects any unknown URLs back to the root.
So that covers the development side of things, but what about in production when it’s hosted behind NGINX?
You would need to change your NGINX config as follows (obviously, your location may vary but if it does, make sure you configure the base URL in React Router accordingly!):
|
|