25

I'm a ruby dev and I just started to learn some Node.js.

I'm running an instance on AWS to host my rails apps with passenger + nginx listening on port 80.

Now I would like to host a node.js app on the same instance (t1-micro) and put it to listen on port 8000.

How can I use Route 53 to create a Record Set to point a subdomain.domain.com to my.ip:8000?

I already tried setting an IPV4 record pointing to my.ip:8000 with no success.

Any idea what I'm doing wrong?

Can I use nginx to serve my nodejs apps?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
gverri
  • 389
  • 1
  • 3
  • 11

3 Answers3

48

This is possible through S3 redirection. Create a S3 bucket with the name

subdomain.domain.com

This bucket does not need to be public. Then, turn on Static Website Hosting in Properties and choose the Redirect response option and configure it with the following options

Target bucket or domain: my.ip:8000
Protocol: Leave this blank

Then go to Route53 and create a Record Set with the following configuration

  • Name: subdomain.domain.com
  • Type: A-IPv4 address
  • Alias: Yes
  • Alias Target: Choose the bucket you just created in S3. If it does not appear wait for it to appear.
  • Routing Policy: Simple
  • Evaluate Target Health: No

And that's it, you should be able to go to subdomain.domain.com and see the changes take effect after roughly 30 seconds. Best of luck!

stelo
  • 696
  • 6
  • 9
  • This worked out very well and is an underappreciated answer. Thank you! – andrasp Jul 24 '18 at 23:52
  • 1
    So, just to understand, the S3 is just a workaround that allows us to set a port, and it has nothing to do with regular "s3 buckets" for storage, right? – Nathan B Nov 18 '18 at 10:33
  • 1
    fantastic - although is it possible to hide the IP address within the url once connected? – brucezepplin Jan 03 '19 at 16:05
  • 2
    the ip address is mapped wrongly in my case the ip is like "http//myip:80//"as you can see that the url misses ":" and has an extra "/" at the end i dono why? any help would be appreciated. – SATYAJEET RANJAN Feb 11 '19 at 13:10
  • sorry guys, does this mean that I have to create a bucket store and pay for another service only for routing? and if I have more than 1 tomcat (or another server) on ec2 instance, I have to create a s3 for each of them? – BohBah Apr 02 '19 at 13:34
  • This is awesome information but I feel like it's not the correct solution for what OP is trying to do. I feel like datasage's answer below is more "correct". – ryangavin Apr 05 '19 at 21:50
  • Why do I get Code: IncorrectEndpoint 400 error : "Message: The specified bucket exists in another region. Please direct requests to the specified endpoint". I made sure I had the bucket location as us-west-2 and picked up the Alias also as s3-website-us-west-2.amazonaws.com – javatogo Jun 04 '19 at 19:56
  • @Stelo: This is wonderful! However this is caching the url updated in S3 and not updating even when I change the redirect response. Please help! – Ankit Goel Jul 02 '20 at 07:41
  • Don't forget about this: When you configure an Amazon S3 bucket for website hosting, you must give the bucket the same name as the record that you want to use to route traffic to the bucket. For example, if you want to route traffic for example.com to an S3 bucket that is configured for website hosting, the name of the bucket must be example.com. from: https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/troubleshooting-s3-bucket-website-hosting.html – ExistMe Dec 06 '20 at 04:00
19

In general, DNS does not care about ports.

You should be able to however, configure nginx to handle both virtual hosts with a reverse proxy to the node.js app. All requests would hit port 80, but how they get handled would depend on the domain.

datasage
  • 19,153
  • 2
  • 48
  • 54
  • Nginx example: http://serverfault.com/questions/208656/routing-to-various-node-js-servers-on-same-machine – crizCraig Aug 13 '14 at 04:17
  • I feel like this is the real answer. I would set up your subdomains to point at the host which has nginx and allow nginx to route the requests based on the subdomain. – ryangavin Apr 05 '19 at 21:49
2

I followed the idea from datasage's answer, and this is how I done it!

On Route 53:

  1. Go to the hosted zone of your domain, click "Create records"
  2. Enter your subdomain under Record name. For example if you want to create "sub.mywebsite.com", type in "sub"
  3. Select CNAME as Record type.
  4. Enter your domain under "Value". For example "mywebsite.com"
  5. Choose a TTL value that is suitable for your use case.
  6. Choose "Simple Routing"
  7. Click "Create records"

And then, create virtual host on your Nginx server:

server {
  server_name sub.mywebsite.com;
  location / {
    proxy_pass http://localhost:xxxx;
  }
}

In fact, you can create as many as you want!

server {
  server_name sub1.mywebsite.com;
  location / {
    proxy_pass http://localhost:xxxx;
  }
}
server {
  server_name sub2.mywebsite.com;
  location / {
    proxy_pass http://localhost:xxxx;
  }
}

And that's it!