Reactjs 如何使用basepath+;nginx
各位! 我正在尝试将react应用程序部署到生产环境 我的案子 在生产环境中,我的应用程序将有一个动态的基本路径,我会在构建过程中更改它 例如:Reactjs 如何使用basepath+;nginx,reactjs,nginx,Reactjs,Nginx,各位! 我正在尝试将react应用程序部署到生产环境 我的案子 在生产环境中,我的应用程序将有一个动态的基本路径,我会在构建过程中更改它 例如: example.com/basepath example.com/basepath2 mypackage.json 。。。 “脚本”:{ “build:basepath”:“PUBLIC\u URL=basepath反应应用程序重新布线的构建”, “build:basepath2”:“PUBLIC\u URL=basepath2反应应用程序重新布线
- example.com/basepath
- example.com/basepath2
。。。
“脚本”:{
“build:basepath”:“PUBLIC\u URL=basepath反应应用程序重新布线的构建”,
“build:basepath2”:“PUBLIC\u URL=basepath2反应应用程序重新布线的构建”,
}
在我的路由中,使用动态basepath设置basename
我的nginx.conf
server {
listen 80;
server_name _;
error_page 400 404 405 =200 @40*_json;
location @40*_json {
default_type application/json;
return 200 '{"code":"1", "message": "Not Found"}';
}
location ~ /basepath{
root /usr/share/nginx/html;
try_files $uri /basepath/index.html;
}
}
我可以通过example.com/basepath访问应用程序。它的工作
如果我访问example.com/basepath/将收到一个空白页
访问example.com/basepath,这些块在example.com/basepath/statics/chank….中下载
访问example.com/basepath/块url是错误的example.com/basepath/basepath/statics/chank
当应用程序链接改变路线时,一切正常
我不知道错误是在nginx中还是在react路由器中
有什么想法吗
对不起,我的英语不好,这是我的第一个主题您是否尝试使用HashRouter而不是BrowserRouter?这是一个经过验证的答案,因此在本例中可能是正确的,但如果您进一步解释此更改解决问题的原因,将对未来用户有所帮助:)