javascript - 将 React 应用程序作为 nginx 中的子路径提供服务
问题描述
有一个在0.0.0.0:5000
本地提供的 React 应用程序。我也有一个域名example.com
。
我想要做的是将example.com/app
目录代理到0.0.0.0:5000
.
我尝试了以下方式。
location /app {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header Host $host;
rewrite /app/(.*) /$1 break;
proxy_pass http://0.0.0.0:5000/app;
}
但在控制台错误显示如下
Loading failed for the <script> with source “http://localhost/static/js/bundle.js”. app:45:1
Loading failed for the <script> with source “http://localhost/static/js/0.chunk.js”. app:45:1
Loading failed for the <script> with source “http://localhost/static/js/main.chunk.js”.
我也尝试添加homepage: './app
到 package.json
我如何解决它 ?
解决方案
nginx 配置对我来说看起来不错,但是您需要更新 webpack 配置以及 React 应用程序中的路由器配置,以便在特定路径上为应用程序提供服务。
我不能对以下要点表示任何赞赏,但它涵盖了您应该需要的所有情况:
https://gist.github.com/codepunkt/ae82a0f3b9deb93908dc1f3d5bbc8da2
(注意你的basePath应该以“/”开头,比如“/app”)
推荐阅读
- r - 如何在 R 中使用 nthroot 函数
- c++ - boost-beast同步http服务器的错误实现
- java - 在 pom 中添加硒依赖项后,AWS Lambda Jar 无法压缩
- python - 当 URL 包含日语字符时,Python 请求不从 API 检索数据
- django - 为托管在 Cloud Foundry 上的应用程序调试 require.txt 文件中给出的 git 包
- mysql - 执行运行时发生 Scala SBT 项目错误
- javascript - SAPUI5:DatePicker 选择了日期,禁用了 InputField 中日期的显示
- time - Gnuplot:为时间戳数据设置 X 轴标签的起点
- python - 用相同大小的数组替换 nan 子数组的 Pythonic 方法
- php - 使用 PHP array_column 在多维数组中查找值