简体   繁体   中英

Node JS “Hello world” server on AWS

I tried to run "Hello world" server on AWS t1.micro instance. What I done:

  • I installed Node on aws
  • Wrote something like this:


require("http").createServer(function(request, response){
      response.writeHeader(200, {"Content-Type": "text/plain"});  
        response.write("Hello World!");  
          response.end();
}).listen(8080);


- Run it on aws: node test_server.js

Now I try to send request from my local machine to server like this:

curl http://NAME:8080 where NAME is public DNS name from aws console, but nothing happens. What I forget? Or what I done wrong

I tried to look for a some kind of tutorial but they describe how to run this on local machine or propose to setup Ngnx. But I look for minimalist example

You need to tell Amazon to authorize inbound traffic on the 8080 port to your instance. See the documentation for the step by step instructions .

In short:

  • Go to the Amazon EC2 console, click on Instance and open the Security Group preference pane
  • Add a new rule authorizing inbound traffic from any IP ( 0.0.0.0/0 ) on port 8080
  • Apply changes: the Node web server should now be able to serve HTTP requests.

@Paul is right, but that was only a part of the solution for me. I was still getting "connection refused" from local machine (remote CURL was fine). So, another part was of the puzzle was solved by turning off the Unix firewall (in addition at AWS security groups configs), ie, iptables!

Are you runing CentOS? Try this:

$ service iptables save
$ service iptables stop
$ chkconfig iptables off

Of course, turning off firewall and opening up AWS console security groups is not a good long-term idea. The proper way is to use iptable, open port 80 for inbound and outbound, and then reroute 80 to Node.js port (3000 or 1337 or something else):

$ sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
$ sudo iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT
$ sudo iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT

You can also use Nginx to do the redirect . Varnish Cache is a good tool to have as well, because it dramatically decreases load on Node.js processes if you have a lot of users hitting one resource/page.

Further reading about AWS and Node.js:

http://www.lauradhamilton.com/how-to-set-up-a-nodejs-web-server-on-amazon-ec2

How to disable iptables on Linux:

http://www.cyberciti.biz/faq/turn-on-turn-off-firewall-in-linux/

Same on CentOS and Fedora:

http://www.cyberciti.biz/faq/disable-linux-firewall-under-centos-rhel-fedora/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM