简体   繁体   English

如何修复在 create-react-app 中找不到的 404

[英]how to fix 404 not found in create-react-app

when I directly visit ' https://xyz/login ', it shows error '404 not found' but it redirect me to ' https://xyz/login ' after logout from home page (' https://xyz/ ').当我直接访问“ https://xyz/login ”时,它显示错误“404 not found”,但它在从主页注销后将我重定向到“ https://xyz/login ”(“ https://xyz/ ”) )。
This problem starts after the deployment of my create-react-app.这个问题是在我的 create-react-app 部署之后开始的。 we used nginx server for deployment.我们使用 nginx 服务器进行部署。

Edit nginx/sites-available/default in VIM OR nano which ever you prefer, on your server machine.在您的服务器机器上编辑您喜欢的VIMnano 中的nginx/sites-available/default。

vi /etc/nginx/sites-available/default
nano /etc/nginx/sites-available/default

Change below下面更改

location / {
    try_files $uri $uri/ =404;
}

to

location / {
    try_files $uri $uri/ /index.html;
}

but it redirect me to 'https://xyz/login' after logout from home page ('https://xyz/').但它在从主页(“https://xyz/”)注销后将我重定向到“https://xyz/login”。

You are able to access login from the logout button because 'create-react-app' and BrowserRouter are only active and able to do routing from / ie homepage.您可以从注销按钮访问登录,因为“create-react-app”和 BrowserRouter 仅处于活动状态并且能够从 / 即主页进行路由。 If you go to /someLink your server does not know what to do with it, then it read a rule to go to 404 error.如果您转到 /someLink 您的服务器不知道如何处理它,那么它会读取一条规则以转到 404 错误。 The above changes all requests to always going to your homepage, that way BrowserRouter can take it from there.以上将所有请求更改为始终访问您的主页,这样 BrowserRouter 可以从那里获取它。

If you want to read more about the issue, check out this question: Discussion如果您想了解有关该问题的更多信息,请查看此问题: 讨论

side note: SEO wise, google still crawls SPAs, but checkout https://www.npmjs.com/package/react-snapshot for SPA optimization.旁注:在 SEO 方面,谷歌仍然会抓取 SPA,但查看https://www.npmjs.com/package/react-snapshot以进行 SPA 优化。

Set basename in BrowserRouter.在 BrowserRouter 中设置 basename。

<BrowserRouter basename='/xyz'>
  <Route exact path='/' /> 
</BrowserRouter>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM