Configure nginx for multiple node.js apps with own domains

Posted by udo on Server Fault See other posts from Server Fault or by udo
Published on 2012-08-04T12:43:13Z Indexed on 2014/06/08 3:28 UTC
Read the original article Hit count: 447

I have a node webapp up and running with my nginx on debian squeeze. Now I want to add another one with an own domain but when I do so, only the first app is served and even if I go to the second domain I simply get redirected to the first webapp. Hope you see what I did wrong here:

example1.conf:

upstream example1.com {
    server 127.0.0.1:3000;
}

server {
listen   80;
server_name  www.example1.com;
rewrite ^/(.*) http://example1.com/$1 permanent;
}

# the nginx server instance
server {
    listen 80;
    server_name example1.com;
    access_log /var/log/nginx/example1.com/access.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    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://example1.com;
      proxy_redirect off;
    }
 }

example2.conf:

upstream example2.com {
    server 127.0.0.1:1111;
}

server {
listen   80;
server_name  www.example2.com;
rewrite ^/(.*) http://example2.com/$1 permanent;
}

# the nginx server instance
server {
    listen 80;
    server_name example2.com;
    access_log /var/log/nginx/example2.com/access.log;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    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://example2.com;
      proxy_redirect off;
    }
 }

curl simply does this:

zazzl:Desktop udo$ curl -I http://example2.com/
HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.2
Date: Sat, 04 Aug 2012 13:46:30 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://example1.com/

Thanks :)

© Server Fault or respective owner

Related posts about nginx

Related posts about debian-squeeze