Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Scenario:

First node.js app is running on 127.0.0.1:8888 and serving only one route 'domain.com/track.gif'

Second node.js app is running on 127.0.0.1:3000 and it's a dashboard for stats gathered by the first app and it's avaliable on 'domain.com' (and other routes like 'domain.com/login' or 'domain.com/dashboard', etc)

When I use this config below - only for first app, the first app works great, it serves this tracking gif file when I hit "domain.com/track.gif":

upstream tracker {
  server 127.0.0.1:8888;
}

server {
  listen 0.0.0.0:80;
  server_name www.domain.com domain.com;
  access_log /var/log/nginx/domain_access.log;
  error_log /var/log/nginx/domain_error.log;

  location /track.gif {
    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://tracker;
    proxy_redirect off;
  }
}

How to add another (second) node.js app to this config to take care of the whole routing for routes like 'domain.com/' or 'domain.com/dashboard' or 'domain.com/login'?

Thanks in advance!

share|improve this question
up vote 4 down vote accepted

Add a second location block. It will match any path except /track.gif.

upstream tracker {
  server 127.0.0.1:8888;
}

server {
  listen 0.0.0.0:80;
  server_name www.domain.com domain.com;
  access_log /var/log/nginx/domain_access.log;
  error_log /var/log/nginx/domain_error.log;

  location /track.gif {
    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://tracker;
    proxy_redirect off;
  }

  location / {
    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;
    proxy_redirect off;
  }
share|improve this answer
    
Exactly what I needed. Thanks! – Pono Feb 6 '12 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .