我正在尝试使用 nginx 部署我的 React 应用。
我遇到的问题是,当改变路线时,例如/about 它实际上并没有更新前端,它只是保留在索引上。
这是我在可用站点中的配置:
server {
listen 80 default_server;
server_name server_ip_here;
root /sites/FrontEnd/React/build;
index index.html;
access_log /var/log/nginx/krim.com.access.log;
error_log /var/log/nginx/krim.com.error.log;
location / {
try_files $uri /index.html;
}
}
这是路由器:
import {Redirect, BrowserRouter as Router,Route,Switch} from 'react-router-dom';
<Router>
<CookieConsent buttonText="Accept">
</CookieConsent>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/About" component={Over}/>
<Route path="/FAQ" component={FAQ}/>
</Switch>
</Router>
现在,页面索引加载了,但是在使用 nginx 时转到 my-ip/About 或 my/FAQ 什么都不做。
现在使用 npm start
一切正常,此外使用 serve -s build
也正常,所以这似乎是我的 nginx 配置错误。
感谢任何帮助,干杯。
编辑:
更正,本地 serve
确实有效,但在远程 vm 上它也不起作用。
最佳答案
您是否尝试过在自己的机器上而不是 VM 上构建它(假设您使用的是虚拟机)?
关于javascript - 使用路由器时,使用 nginx 的 React 应用程序不会切换组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56742657/