Host Multiple Sites on Single EC2 Instance - Ubuntu 14.04, nginx, node js

Tutorial

This post is for you if you are trying to host multiple websites on the same EC2 instance. The tutorial is tailored for Ubuntu, but other than the apt-get instructions, it should work for all Linux instances.

Briefly speaking, we want to use nginx to reverse proxy the traffic to different ports on the same instance. Nginx allows you to direct traffic from different domain name to different ports on your server.

Step1: Set EC2 Elastic IP
  1. Go to your EC2 console > Network & Security > Elastic IPs
  2. Allocate New Address (1.2.3.4)
  3. Associate Address to your EC2 instance
    Bonus - Elastic IP is free (6/23/15) as long as you associate it
Step2: Direct DNS Provider to EC2 IP

Go to your DNS provider (I personally recommend namecheap for good deals) and direct the 'A record' to your Elastic IP. This basically tells the DNS to direct your domain name to your EC2 IP. You want to point both of your domains to same IP:

(example1.com) -> (1.2.3.4)

(example2.com) -> (1.2.3.4)

If you are interested in wht the 'A record' is and why I choose it over other options, check out this post.


Note - You will need to wait up to 24 hrs for the DNS to propagate the redirect before going on the next step.

Step3: Start Nodejs Server on Different Ports

(This step assumes you have node and npm installed)

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

var server = app.listen(3030); //USE DIFFERENT PORTS

I used the above code to host a basic site on port 3030. I'll host two sites on ports 3030 and 3040 for this example.
Start your server with npm start

Step4: Install & Configure nginx
sudo apt-get update
sudo apt-get install nginx

nginx generates an important file that routes HTTP traffic to your system, edit it to change nginx configuration:

sudo vi /etc/nginx/sites-available/default

Delete the content in default and replace it with the following configuration:

server {
    listen 80;
    server_name example1.com;
    location / {
        proxy_pass http://localhost:3030;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}


server {
    listen 80;
    server_name example2.com;
    location / {
        proxy_pass http://localhost:3040;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Restart your nginx

sudo nginx -s reload

example1.com now goes to localhost:3030, and example2.com goes to localhost:3040!

There is a bunch of customization you can do here, I'll just keep it to the barebone and leave it for you to explore on nginx.org

Troubleshooting
Open EC2 HTTP Port (80)

You might not have opened your HTTP Port for external access.

  1. Go to EC2 console > Network & Security > Security Groups
  2. Find the security group associated with your EC2 instance
  3. Go to Inbound, and open HTTP access for all sources (0.0.0.0/0)
Kill iptables redirects

You might have iptables redirecting your traffic in front of nginx. Kill them, I wouldn't use iptables with nginx.

You can see if you are running any process with sudo iptables --list
This post might help you find a solution to kill the iptables, or more simply, just sudo reboot