Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up multiple node servers using Nginx without subdomains

I have been looking all over but haven't found a single person hosting multiple node sites on the same server without using subdomains. I want something like the following...

website.com/app1 -> 127.0.0.1:3000
website.com/app2 -> 127.0.0.1:9000

upstream node {
   server 127.0.0.1:3000;
   keepalive 64;
}

server {
    listen 80;
    server_name  webaddress.com;

    root /var/www/trucks/;

    location /livereload {

        proxy_pass http://localhost:35729;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    location /app1 {
        rewrite ^/app1/?(.*) /$1 break;
        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_pass http://127.0.0.1:3000;
        root /var/www/trucks;
}

I have even tried rewriting the request; however, that doesn't appear to be having any effect. If I remove /app1 it works correctly. The closest I have gotten is forwarding to multiple sites but then the local express routes are invalid.

like image 716
slimbo Avatar asked Nov 22 '13 02:11

slimbo


1 Answers

I have a working setup as follows:

server {
  listen 8080;
  server_name localhost;
  location / {
    proxy_pass http://localhost:3000;
    proxy_set_header Host $host;
  }
  location /api {
    proxy_pass http://localhost:3001;
    rewrite ^/api(.*) /$1 break;
    proxy_set_header Host $host;
  }
}
like image 56
mtsr Avatar answered Oct 20 '22 04:10

mtsr