简体   繁体   English

具有2个NodeJS应用程序的NGINX服务器

[英]NGINX server with 2 NodeJS Apps

I will make it short without introductions .. 我将简短地介绍一下..

I'm having a serious issues with NGINX configuration (on google cloud) to make 2 nodejs apps working on the same domain with different PORTS 我在NGINX配置上存在严重问题(在Google云上),以使2个nodejs应用程序在具有不同端口的同一域上工作

let's say app1 is working on port 3002, app2 working on port 3003 假设app1在端口3002上运行,app2在端口3003上运行

app1 app1

    location / {
        root /home/bitnami/project_name;
        proxy_pass http://127.0.0.1:3002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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;
    }

app2 app2

   location /app2 {
        root /home/bitnami/project_name;
        proxy_pass http://127.0.0.1:3003;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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;
    }

when I surf www.example.com/app2, I get 404 page 当我浏览www.example.com/app2时,我得到404页

I know some of u will say that this Q has been asked before, believe me I've tried all possible solutions on stackoverflow .. non has worked with me 我知道您中的某些人会说这个问号已经被问过了,相信我我已经在stackoverflow上尝试了所有可能的解决方案。

Note: app1 location it has to be the main domain so (/) the main domain URL without path 注意:app1的位置必须是主域,因此(/)没有路径的主域URL

I believe your code does not use relative paths, thats why you are getting this error, add this line: 我相信您的代码未使用相对路径,这就是为什么您遇到此错误,请添加以下行:

rewrite ^/app2(.*) /$1 break;

and no root required for proxy pass, your new code shall look like this: 并且不需要root即可通过代理,因此您的新代码应如下所示:

location /app2 {
    #root /home/bitnami/project_name;
    proxy_pass http://127.0.0.1:3003;
    #proxy_http_version 1.1;
    #proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    #proxy_set_header X-NginX-Proxy true;
    #proxy_redirect off;
    rewrite ^/app2(.*) /$1 break;
}

The first location block captures requests for all requests of your domain, leaving the second block never used. 第一个location块捕获对您域中所有请求的请求,而第二个位置块从未使用。 Put the second block before the first one and it should work. 将第二个块放在第一个块之前,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM