Javascript 在nginx中将React应用程序用作子路径
有一个React应用程序,在本地Javascript 在nginx中将React应用程序用作子路径,javascript,reactjs,nginx,mod-rewrite,create-react-app,Javascript,Reactjs,Nginx,Mod Rewrite,Create React App,有一个React应用程序,在本地0.0.0:5000上提供。 我还有一个域名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;
0.0.0:5000
上提供。
我还有一个域名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”.
为具有源的加载失败”http://localhost/static/js/bundle.js”. 应用程序:45:1
加载源为“”的文件失败http://localhost/static/js/0.chunk.js”. 应用程序:45:1
加载源为“”的文件失败http://localhost/static/js/main.chunk.js”.
我还尝试将主页:'./app
添加到package.json中
如何修复它?nginx配置在我看来还行,但您需要更新React应用程序中的网页配置以及路由器配置,以便在特定路径上为应用程序提供服务 我不能相信以下要点,但它涵盖了您需要的所有案例:
(请注意,您的基本路径应以“/”开头,例如“/app”)我认为nginx配置正常,但您需要更新React应用程序中的网页配置以及路由器配置,以便在特定路径上为应用程序提供服务 我不能相信以下要点,但它涵盖了您需要的所有案例:
(请注意,您的基本路径应该以“/”开头,例如“/app”)尝试将/app更改为/app。如果您使用的是react路由器,您还需要在路由器上设置basename字段,并将./app更改为/app。如果您使用的是react路由器,您还需要在路由器上设置basename字段,无需配置webpack。只需将主页添加为/app即可。重写规则执行REST无需配置网页包。只需将主页添加为/app即可。重写规则做其余的