Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在网站的子路由上托管react应用程序时出现问题-多个网站一个IP地址NGINX_Reactjs_Amazon Web Services_Ubuntu_Nginx_Pm2 - Fatal编程技术网

Reactjs 在网站的子路由上托管react应用程序时出现问题-多个网站一个IP地址NGINX

Reactjs 在网站的子路由上托管react应用程序时出现问题-多个网站一个IP地址NGINX,reactjs,amazon-web-services,ubuntu,nginx,pm2,Reactjs,Amazon Web Services,Ubuntu,Nginx,Pm2,我是NGINX新手,所以这可能是一个简单的修复,但我找不到任何关于我在这里尝试做什么的好文档 所以我有一个网站,由nginx和pm2托管在ubuntuaws服务器上。我的问题是,我想为www.mysite.com上的网站和www.mywebsite.com/app上的react应用程序提供服务。这似乎不是最难做的事情。然而,我一直有问题,不能让它工作,因为我想。(部分原因是我还希望在www.mywebsite.com/API上托管一个API服务器,但这是此后的一个问题)。我首先能够通过以下ngi

我是NGINX新手,所以这可能是一个简单的修复,但我找不到任何关于我在这里尝试做什么的好文档

所以我有一个网站,由nginx和pm2托管在ubuntuaws服务器上。我的问题是,我想为
www.mysite.com
上的网站和
www.mywebsite.com/app
上的react应用程序提供服务。这似乎不是最难做的事情。然而,我一直有问题,不能让它工作,因为我想。(部分原因是我还希望在
www.mywebsite.com/API
上托管一个API服务器,但这是此后的一个问题)。我首先能够通过以下nginx配置在
www.mysite.com
上托管react应用程序:

server {
    listen 80;
    server_name 18.191.56.251;
    root /home/ubuntu/app/src;
location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_max_temp_file_size 0;
    proxy_pass http://127.0.0.1:3000/;
    proxy_redirect off;
    proxy_read_timeout 240s;
}
}
但当我通过改变配置将其切换到子路由时:

server {
    listen 80;
    server_name x.x.x.x;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        proxy_set_header   X-Forwarded-Proto $scheme;

        proxy_connect_timeout       600;
        proxy_send_timeout          600;
        proxy_read_timeout          600;
        send_timeout                600;

    location /status {
        return 200;
    }

    location /app {
        proxy_pass http://127.0.0.1:3000;
    }

    location / {
        proxy_pass http://...:3011/;
    }
}
然后,我的静态网站的基本路径
www.mywebsite.com
可以正常工作,但是
www.mywebsite.com/app
不能并“加载”一个空白页面,但如果您检查,您会发现该应用程序无法加载某些资源:

bundle.js:1
Failed to load resource: the server responded with a status of 404 (Not Found)
0.chunk.js:1 
Failed to load resource: the server responded with a status of 404 (Not Found)
main.chunk.js:1 
Failed to load resource: the server responded with a status of 404 (Not Found)
编辑更多研究: 如果我检查资源请求,我会看到它的url是
www.mywebsite.com/static/js/XXX.js
,但是静态文件夹位于
~/app/build/static
。这是nginx还是pm2托管静态文件的问题


非常感谢您的帮助,谢谢

因此我仍然希望得到与此不同的结果,但我找到了解决方法,我在
位置/app
之后添加了以下内容:

location /static {
     proxy_pass http://127.0.0.1:3000;
}

我不喜欢这个解决方案,因为如果我最终将静态内容作为其他项目的一部分,那么它将不起作用,但不知何故,我网站的所有资源都打包在
/
的正确代理中。所以现在一切都在运转

您是否尝试在
代理\u过程后添加斜杠http://127.0.0.1:3000
?i、 e.代理通行证
http://127.0.0.1:3000/