21

I have a couple of small production sites and a bunch of fun hobbyist/experimental apps and such. I'd like to run all of them on one EC2 instance.

Can I install node.js, npm, express and couchdb once, and then run each app on a different port, and adjust the dns settings at my domain registry to point to the appropriate place?

Update: Thanks Mike! For anyone else who's looking for multiple IP addresses on EC2: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html

Costa Michailidis
  • 7,691
  • 15
  • 72
  • 124

2 Answers2

41

There are multiple ways to go about it.

Different Ports

You could run each Node.js process on a different port and simply open the ports to the world. However, your URLs would need a port on the end of the hostname for each project. yoururl.com:8080/ would technically work, but probably not what you're looking for.

Multiple IP Addresses

You could use multiple IP addresses on one EC2 instance, however, they come with an additional cost of about $3.65 a month each. So if you have 10 different domains you want to host on once instance then that's over $30 a month extra in hosting fees.

On the flip side, any domain using SSL needs it's own IP address.

Also, there are limits to the number of IP addresses you can assign to an instance and the smaller the instance, the less IP addresses you get.

The number of IP addresses that you can assign varies by instance type. Small instances can accommodate up to 8 IP addresses (across 2 elastic network interfaces) whereas High-Memory Quadruple Extra Large and Cluster Computer Eight Extra Large instances can be assigned up to 240 IP addresses (across 8 elastic network interfaces). For more information about IP address and elastic network interface limits, go to Instance Families and Types in the Amazon EC2 User Guide.

Express Vhosts

Express comes with virtual host functionality. You can run multiple domains under one Node.js/Express server and setup routes based on domain name. vhost under Express enables this.

Reverse Proxy

You can setup Nginx in front of multiple application servers. This has the most flexibility. You can have one Node.js process per domain which allows you to do updates and restarts on one domain at a time. It also allows you to host applications servers such as Apache/PHP under the same EC2 instance along side your Node.js process.

With Nginx acting as a reverse proxy you could also host different application servers under the same domain, but serving different paths.

For instance, Node.js could serve the main root path of a domain but you could setup the /blog/ path to go to a Apache/PHP/Wordpress setup on the same EC2 instance.

Daniel
  • 38,041
  • 11
  • 92
  • 73
  • 1
    You rock! Nginx sounds like the best place to go next. I know very little about it, so I'll Google around. What about using separate IPs? – Costa Michailidis May 14 '13 at 17:35
  • 2
    Updated answer regarding mutliple IPs. – Daniel May 14 '13 at 18:10
  • Thanks so much Daniel! Great stuff! I'll need new IPs for anything with SSL, that's for sure. Maybe soon Amazon will adopt ipv6 and lower the cost of new IPs. – Costa Michailidis May 14 '13 at 18:25
  • @Daniel do you have any advice on how to deploy when you've got 3 apps running using a reverse proxy? When i use "eb deploy" for one app it puts it in the /var/current folder and erases my nginx proxy file back to the default for some reason. Obviously you can't deploy all 3 apps this way so is it bad to just rsync? – neptunian Jun 12 '16 at 20:38
  • @Daniel Thanks for your answer. Through which of your methods I can run multiple node app/servers on the same domain, i mean my primary domain get served using express. and i have another small app which is on node but uses websockets ws. How can I run both together such that www.mysite.com should be served by express node app and mysite.com/hello should serve the web sockets node app? – Faizan Jul 30 '16 at 10:17
5

Already answered @ https://stackoverflow.com/a/50528580/3477353 but mentioning it again.

Adding to the @Daniel answer, I would like to mention the configuration of my nginx for those who are looking for the exact syntax in implementing it.

server {
  listen 80;
  server_name mysite.com;
  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $http_host;
    proxy_pass        http://127.0.0.1:3000;
  }
}
server {
  listen 80;
  server_name api.mysite.com;
  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $http_host;
    proxy_pass        http://127.0.0.1:4500;
  }
}

Just create two server objects with unique server name and the port address.

Mind proxy_pass in each object.

Thank you.

Balasubramani M
  • 7,742
  • 2
  • 45
  • 47