Q:

Reverse proxy with nginx on the same port

I'm more a programmer than a server admin, so the question might seem trivial to some, but I can't get my nginx reverse proxy to forward my requests to the right server... First, here's my config file (in sites-available, with a symlink in sites-enabled)
upstream nodejs1 {
    server 127.0.0.1:3000;
}

upstream nodejs2 {
    server 127.0.0.1:3001;
}

server {
    server_name nodejs_proxy;
    listen 0.0.0.0:80;
    access_log /var/log/nginx/nodejs.log;
    location /otherlocation/ {
            rewrite /otherlocation/(.*) /$1 break;
            proxy_pass      http://nodejs1/;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header   X-NginX-Proxy true;
    }

    location / {
            proxy_pass      http://nodejs2/;
            proxy_redirect  off;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header   X-NginX-Proxy true;
    }
}

So if I try to access http://myserver.com/, everything works fine and I'm redirected on port 3001. However, if I try to access http://myserver.com/otherlocation/, I'm also redirected to port 3001 (so I get a 404).
Is there something in the above snippet I don't do correctly to be redirected to the right server when I try to access http://myserver.com/otherlocation/?
Thank you
PS : myserver.com is a temporary domain that's prone to change multiple times (dev, pre-prod, prod) so that's why I'm trying to write something what will work on all environments

A:

First of all you should check that the right server is handling the request and debug what's most important. In your nginx.conf file (not in sites-enabled) inside the http block place this 
log_format mcombined '$server_name - $host - $remote_addr - $remote_user [$time_local]  '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';

The above is the default log format of nginx with the addition of $server_name and $host. You may add any variable you think may be useful.
Then change your sites-enabled conf to use this log format:
access_log /var/log/nginx/nodejs.log mcombined;

But the real problem is this: Inside your /otherlocation/ block, you have a rewrite rule that rewrites everything to /, so when the requests come to the /otherlocation/ block, they are redirected to /. 
So your final sites-enabled conf will look like this:
upstream nodejs1 {
  server 127.0.0.1:3000;
}

upstream nodejs2 {
  server 127.0.0.1:3001;
}

server {
  server_name nodejs_proxy;
  listen 0.0.0.0:80;
  access_log /var/log/nginx/nodejs.log mcombined;
  location ^~ /otherlocation/ {
        proxy_pass      http://nodejs1/;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   X-NginX-Proxy true;
  }

  location / {
        proxy_pass      http://nodejs2/;
        proxy_redirect  off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   X-NginX-Proxy true;
  }
}

