在启用 SSL 的情况下刷新 URL 时出现react-router问题

IT技术 javascript reactjs ssl nginx react-router
2021-04-28 23:09:03

目前,我在使用 react-router 的 BrowserHistory 和 nginx 代理转发请求时遇到问题。我已阅读以下答案:

手动刷新或写入时,React-router url 不起作用

似乎我正在寻找的解决方案是一个包罗万象的解决方案,/*将所有传入的请求转发到index.html.

如果 URL 是一层深,这可以正常工作,即。/about. 但是,如果我尝试在子路由上刷新页面/some/other/url,则页面会完全中断。

这是我当前的 nginx 配置(注意 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;
}

location /块中,您会注意到try_files我试图开始工作但无济于事的其他一些语句。

下面是什么react-router逻辑是这样的:

<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}
      />
      .
      .
      .
      .

任何帮助或指导表示赞赏。

1个回答

我遇到了和你类似的问题,我的团队花了很多时间研究 nginx 和 react-router,这与你的设置非常相似。

我也有以下错误:

未捕获的语法错误:意外标记 < 错误

最后我找到了一个解决方案,这实际上是在我的 index.html 服务器文件中。

我正在为我的包使用相对路径

<link href="styles.css" rel="stylesheet" />    
<script src="bundle.js"></script>

在路由之后,假设您尝试访问your/path/subdomain,它会尝试在your/path/subdomain/styles.css处获取 bundle.js 和 style.css 文件your/path/subdomain/bundle.js,但它们位于我的项目的根目录中。

所以我的解决方案就是使用

<link href="/styles.css" rel="stylesheet" />
<script src="/bundle.js"></script>

这解决了这个问题。希望这对你有帮助。