Currently I'm experiencing problems using react-router's BrowserHistory with an nginx proxy to forward requests. I've read the following answer:
React-router urls don't work when refreshing or writting manually
And it seems that the solution I'm looking for is a catch-all, /*
to forward all of the incoming requests to index.html
.
This works fine if the URL is one level deep, ie. /about
. However, if I try to refresh the page while on a child-route, /some/other/url
, the page completely breaks.
Here's my current nginx configuration (notice the permanent redirect for http -> https):
server {
listen 443 ssl;
server_name my.domain.io;
ssl_certificate /etc/letsencrypt/live/my.domain.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/my.domain.io/privkey.pem;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
#try_files $uri $uri/ /index.html;
try_files $uri $uri/ /index.html$is_args$args;
#try_files $uri /index.html;
#try_files $uri $uri/ /index.html?$args;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location ~ /.well-known {
allow all;
}
}
server {
listen 80;
server_name my.domain.io;
return 301 https://$host$request_uri;
}
In the location /
block, you'll notice some other try_files
statements that I've tried to get to work, but to no avail.
Here's what the react-router
logic looks like:
<Route path="/user" component={ConnectedUserPage} />
<Route path="/user/preview/:locationId" component={ConnectedUserPage} />
<Route
path="/user/preview/:locationId/menu"
component={ConnectedUserPage}
fullMenuVisible={true}
/>
<Route
path="/user/preview/:locationId/menu/:sectionName/:someId"
component={ConnectedUserPage}
dishDetailsVisible={true}
/>
{/* Restaurant Profile */}
<Route
path="/location-profile/:profileId"
component={LocationProfile}
activeTabIndex={0}
/>
<Route
path="/location-profile/:profileId/gallery"
component={LocationProfile}
activeTabIndex={0}
/>
<Route
path="/location-profile/:profileId/gallery/:imageId"
component={LocationProfile}
activeTabIndex={0}
/>
<Route
path="/location-profile/:profileId/details"
component={LocationProfile}
activeTabIndex={1}
/>
<Route
path="/location-profile/:profileId/menu"
component={LocationProfile}
activeTabIndex={2}
/>
<Route
path="/location-profile/:profileId/comments"
component={LocationProfile}
activeTabIndex={3}
/>
<Route
path="/location-profile/:profileId/stuff"
component={LocationProfile}
activeTabIndex={4}
/>
<Route
path="/location-profile/:profileId/menu/:somethingName"
component={LocationProfile}
activeTabIndex={2}
/>
<Route
path="/location-profile/:profileId/menu/:somethingName/:someItemId"
component={LocationProfile}
activeTabIndex={2}
/>
.
.
.
.
Any help or guidance is appreciated.