简体   繁体   中英

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

let's say app1 is working on port 3002, app2 working on port 3003

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

   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

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

Note: app1 location it has to be the main domain so (/) the main domain URL without path

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:

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. Put the second block before the first one and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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