How to run multiple node application with NGINX: same droplet same domain

July 21, 2014 20.2k views

I followed the tutorial here with node and NGINX.
They work great.
But, they all are running the node application from the root of the domain.

I have a number of small node apps and I want to be able to run them like:

http://mydomain/app1
http://mydomain/app2

where app1 may be localhost:9000 and app2 is localhost:9001 etc.

when I change my hosts-enabled file from
location / to location /app1/

server {
    listen 0.0.0.0:80;
    server_name 192.241.223.202;
    access_log /var/log/nginx/ddApp.log;

    location /app1/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:9000;
        proxy_redirect off;
    }
}

my application is launched (node sends down the html document) but the browser can't follow any of the links (load any resources ..404's).

This direct link can get the resouces:

http://192.241.223.202:9000/app/b7672478.app.js

but through the app1 path

http://192.241.223.202/app1/app/b7672478.app.js

NGINX just serves up the root html document.

Actually all requests to the app1 path produce the same response.

The NGINX docs just make my head hurt (as well as my pride).

Any help and direction would be great.

P:)

6 Answers

Oh happy days!
We are up with app1! ... other than a couple of bootstrap fronts out of the bower component failing to pick up the app1 base .... I'll deal with it later.
Also the <base> tag needs a trailing slash so for this baby it was:

<base href="/app1/">

THANK YOU SO MUCH!!!

Try adding

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

under

location /app1/ {

and restarting nginx. Does that work?

server {
    listen 0.0.0.0:80;
    server_name 192.241.223.202;
    access_log /var/log/nginx/ddApp.log;

    location /app1/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:9000;
        proxy_redirect off;
        rewrite ^/app1/(.*)$ /$1 break;
    }
}

nginx -s reload

http://192.241.223.202/app1/ in a new browser window is still broken.

  • Does your application log requests/visits? Can you check what path nginx passed to it?

root@test:/etc/nginx/sites-enabled# tail /var/log/nginx/ddApp.log
82.166.118.134 - - [21/Jul/2014:15:31:07 -0400] "GET /socket.io/socket.io.js HTTP/1.1" 404 208 "http://192.241.223.202/app1/" "Mozilla/5.0 (X11; Linux x8664) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
82.166.118.134 - - [21/Jul/2014:15:31:07 -0400] "GET /app/77f2a613.vendor.js HTTP/1.1" 404 208 "http://192.241.223.202/app1/" "Mozilla/5.0 (X11; Linux x86
64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
82.166.118.134 - - [21/Jul/2014:15:31:07 -0400] "GET /app/b7672478.app.js HTTP/1.1" 404 208 "http://192.241.223.202/app1/" "Mozilla/5.0 (X11; Linux x8664) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
82.166.118.134 - - [21/Jul/2014:15:31:14 -0400] "GET /app1/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86
64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
82.166.118.134 - - [21/Jul/2014:15:31:14 -0400] "GET /app/77f2a613.vendor.js HTTP/1.1" 404 208 "http://192.241.223.202/app1/" "Mozilla/5.0 (X11; Linux x8664) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
82.166.118.134 - - [21/Jul/2014:15:31:14 -0400] "GET /socket.io/socket.io.js HTTP/1.1" 404 208 "http://192.241.223.202/app1/" "Mozilla/5.0 (X11; Linux x86
64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
82.166.118.134 - - [21/Jul/2014:15:31:14 -0400] "GET /app/b7672478.app.js HTTP/1.1" 404 208 "http://192.241.223.202/app1/" "Mozilla/5.0 (X11; Linux x8664) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
82.166.118.134 - - [21/Jul/2014:15:31:14 -0400] "GET /app/58c604fc.app.css HTTP/1.1" 404 208 "http://192.241.223.202/app1/" "Mozilla/5.0 (X11; Linux x86
64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
82.166.118.134 - - [21/Jul/2014:15:31:14 -0400] "GET /app/088ba864.vendor.css HTTP/1.1" 404 208 "http://192.241.223.202/app1/" "Mozilla/5.0 (X11; Linux x8664) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
82.166.118.134 - - [21/Jul/2014:15:31:30 -0400] "GET /app1/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86
64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"
root@test:/etc/nginx/sites-enabled#

  • I meant on the node.js end. If you're using express version 3, you can add this to enable request logging:

    app.use(express.logger('dev')); 
    

    or

    app.use(require('morgan')('dev')); // make sure you install the "morgan" package.
    

    if you're using express version 4.

sorry .... I've added morgan (I'm on Express 4) I only seem to get a
Get / 304 with
http://192.241.223.202/app1/
and then the broken app is displayed in the browser

Here is a session:

root@test:/var/log/upstart# service ddApp stop
ddApp stop/waiting
root@test:/var/log/upstart# service ddApp start
ddApp start/running, process 7080
root@test:/var/log/upstart# tail ddApp.log -f
league list
GET /api/leagueList 304 160ms
GET /api/leagueList 304 161ms
GET / 304 157ms
GET / 304 159ms
GET / 304 164ms
info - socket.io started
loading league.controller
Express server listening on 9000, in production mode
db connection open
GET / 304 181ms
GET / 304 164ms <-- one of these for each refress of the browser at http://192....../app1

  • Ok, so the rewrite rule is actually working properly.

    http://192.241.223.202/app1/app/77f2a613.vendor.js works while http://192.241.223.202/app/77f2a613.vendor.js doesn't, so you need to update your layout view and replace

    <base href="/">
    

    with

    <base href="/app1">
    

I spent hours and hours trying to solve this same problem. Knowing those hours are wasted because I needed a trailing slash is heartbreaking, but so glad I found this post.

Have another answer? Share your knowledge.