Nginx proxy for Nodejs

This blog is running in a simple vps machine on OVH with other Nodejs applications, each one exposed on his own port. To manage all this applications the best solution I found for that it's Nginx. Its really simple to use, so let's go:

First install Nginx:

apt-get install nginx

Be sure that port 80 its free.

The apps are don't have to be running on the same server always so add it to host file:

/etc/hots

93.10.16.192    leoskimdo.com blog.leoskimdo.com 

Now configure the site that its running on port 81:

/etc/nginx/sites-available/leoskimdo.com

upstream leoskimdo.com {
    server leoskimdo.com:81;
}

server {
  listen 0.0.0.0:80;
  server_name leoskimdo.com;
  access_log /var/log/nginx/leoskimdo_access.log;
  error_log /var/log/nginx/leoskimdo_error.log debug;

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://leoskimdo.com;
    proxy_redirect off;
  }
}

Same process with the blog, witch is running on port 82

upstream blog.leoskimdo.com {
    server blog.leoskimdo.com:82;
}

server {
  listen 0.0.0.0:80;
  server_name blog.leoskimdo.com;
  access_log /var/log/nginx/blog_leoskimdo_access.log;
  error_log /var/log/nginx/blog_leoskimdo_error.log debug;

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://blog.leoskimdo.com;
    proxy_redirect off;
  }
}

Last step is restart the web server:

service nginx restart

And you are done.