问题托管反应应用程序在网站的子路径 - 多个网站一个IP地址NGINX

问题描述 投票:2回答:1

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

所以我有一个由nginx和pm2托管在ubuntu aws服务器上的网站。我的问题是,我想在www.mysite.comwww.mywebsite.com/app的反应应用程序上提供服务。这似乎不应该是最困难的事情。但是我遇到了问题,无法按照我的意愿让它工作。 (部分原因是因为我也希望在www.mywebsite.com/api上主持一个API服务器,但这是一个问题)。我能够首先在www.mysite.com上使用以下nginx配置主持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)

编辑更多的研究:如果我检查资源的请求,我看到它的网址是www.mywebsite.com/static/js/XXX.js但静态文件夹是在~/app/build/static。这是托管静态文件的nginx还是pm2问题?

任何帮助表示赞赏,谢谢

reactjs amazon-web-services ubuntu nginx pm2
1个回答
0
投票

所以我仍然会喜欢不同的结果,但我想出了一个解决方法,我在location /app之后添加了以下内容:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.