简体   繁体   English

AWS上的节点JS“ Hello world”服务器

[英]Node JS “Hello world” server on AWS

I tried to run "Hello world" server on AWS t1.micro instance. 我试图在AWS t1.micro实例上运行“ Hello world”服务器。 What I done: 我做了什么:

  • I installed Node on aws 我在AWS上安装了Node
  • 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 -在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. curl http://NAME:8080 ,其中NAME是来自AWS控制台的公共DNS名称,但没有任何反应。 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. 我试图寻找某种教程,但他们描述了如何在本地计算机上运行此教程或建议设置Ngnx。 But I look for minimalist example 但我寻找简约的例子

You need to tell Amazon to authorize inbound traffic on the 8080 port to your instance. 您需要告诉亚马逊授权您的实例在8080端口上的入站流量。 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 转到Amazon EC2控制台,单击实例,然后打开安全组首选项窗格
  • Add a new rule authorizing inbound traffic from any IP ( 0.0.0.0/0 ) on port 8080 添加新规则,授权来自端口8080上任何IP( 0.0.0.0/0 )的入站流量
  • Apply changes: the Node web server should now be able to serve HTTP requests. 应用更改:Node Web服务器现在应该能够处理HTTP请求。

@Paul is right, but that was only a part of the solution for me. @Paul是正确的,但这对我来说只是解决方案的一部分。 I was still getting "connection refused" from local machine (remote CURL was fine). 我仍然从本地计算机收到“连接被拒绝”(远程CURL很好)。 So, another part was of the puzzle was solved by turning off the Unix firewall (in addition at AWS security groups configs), ie, iptables! 因此,难题的另一部分是通过关闭Unix防火墙(此外在AWS安全组配置中)(即iptables)解决了!

Are you runing CentOS? 您在运行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. 当然,关闭防火墙并打开AWS控制台安全组不是一个长期的好主意。 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): 正确的方法是使用iptable,打开端口80进行入站和出站,然后将80重新路由到Node.js端口(3000或1337或其他):

$ 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 . 您也可以使用Nginx进行重定向。 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. Varnish Cache也是一个很好的工具,因为如果有很多用户访问一个资源/页面,它会大大降低Node.js进程的负载。

Further reading about AWS and Node.js: 进一步了解AWS和Node.js:

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

How to disable iptables on Linux: 如何在Linux上禁用iptables:

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

Same on CentOS and Fedora: 在CentOS和Fedora上也一样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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