Report this

What is the reason for this report?

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

Posted on July 21, 2014

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:)



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Try adding

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

under

location /app1/ {

and restarting nginx. Does that work?

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 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/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 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 /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 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 /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 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/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 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: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#

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.