简体   繁体   English

如何配置对EC2实例的直接http访问?

[英]How to configure direct http access to EC2 instance?

This is a very basic Amazon EC2 question, but I'm stumped so here goes. 这是一个非常基本的亚马逊EC2问题,但我很难过这里。

I want to launch an Amazon EC2 instance and allow access to HTTP on ports 80 and 8888 from anywhere. 我想启动一个Amazon EC2实例,并允许从任何地方访问端口80和8888上的HTTP。 So far I can't even allow the instance to connect to on those ports using its own IP address (but it will connect to localhost). 到目前为止,我甚至不能允许实例使用自己的IP地址连接到这些端口(但它将连接到localhost)。

I configured the "default" security group for HTTP using the standard HTTP option on the management console (and also SSH). 我使用管理控制台(以及SSH)上的标准HTTP选项为HTTP配置了“默认”安全组。

I launched my instance in the default security group. 我在默认安全组中启动了我的实例。

I connected to the instance on SSH port 22 twice and in one window launch an HTTP server on port 80. In the other window I verify that I can connect to HTTP using the "localhost". 我连接到SSH端口22上的实例两次,并在一个窗口中启动端口80上的HTTP服务器。在另一个窗口中,我验证我可以使用“localhost”连接到HTTP。

However when I try to access HTTP from the instance (or anywhere else) using either the public DNS or the Private IP address I het "connection refused". 但是,当我尝试使用公共DNS或私有IP地址从实例(或其他任何地方)访问HTTP时,我认为“连接被拒绝”。

What am I doing wrong, please? 我做错了什么,拜托?

Below is a console fragment showing the wget that succeeds and the two that fail run from the instance itself. 下面是一个控制台片段,显示成功的wget和从实例本身运行失败的两个。

--2012-03-07 15:43:31--  http://localhost/
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: /__whiff_directory_listing__ [following]
--2012-03-07 15:43:31--  http://localhost/__whiff_directory_listing__
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: “__whiff_directory_listing__”

[ <=>
                                                                                                               ] 7,512       --.-K/s   in 0.03s   

2012-03-07 15:43:31 (263 KB/s) - “__whiff_directory_listing__” saved [7512]

[ec2-user@ip-10-195-205-30 tmp]$ wget http://ec2-50-17-2-174.compute-1.amazonaws.com/
--2012-03-07 15:44:17--  http://ec2-50-17-2-174.compute-1.amazonaws.com/
Resolving ec2-50-17-2-174.compute-1.amazonaws.com... 10.195.205.30
Connecting to ec2-50-17-2-174.compute-1.amazonaws.com|10.195.205.30|:80... failed:          
Connection refused.
[ec2-user@ip-10-195-205-30 tmp]$ wget http://10.195.205.30/
--2012-03-07 15:46:08--  http://10.195.205.30/
Connecting to 10.195.205.30:80... failed: Connection refused.
[ec2-user@ip-10-195-205-30 tmp]$ 

(0) It's silly but the first thing you need to do is to make sure that your web server is running. (0)这很愚蠢,但您需要做的第一件事就是确保您的Web服务器正在运行。

(1) You need to edit your Security Group to let incoming HTTP packets access your website. (1)您需要编辑安全组以允许传入的HTTP数据包访问您的网站。 If your website is listening on port 80, you need to edit the Security Group to open access to port 80 as mentioned above. 如果您的网站正在侦听端口80,则需要编辑安全组以打开对端口80的访问,如上所述。 If your website is listening on some other port, then you need to edit the Security Group to access that other port. 如果您的网站正在侦听其他端口,则需要编辑安全组以访问该其他端口。

(2) If you are running a Linux instance, the iptables firewall may be running by default. (2)如果您正在运行Linux实例,则iptables防火墙可能默认运行。 You can check that this firewall is active by running 您可以通过运行检查此防火墙是否处于活动状态

sudo service iptables status sudo服务iptables状态

on the command line. 在命令行上。 If you get output, then the iptables firewall is running. 如果输出,则iptables防火墙正在运行。 If you get a message " Firewall not running ", that's pretty self-explanatory. 如果您收到消息“ 防火墙未运行 ”,那就非常明显了。 In general, the iptables firewall is running by default. 通常, iptables防火墙默认运行。

You have two options: knock out the firewall or edit the firewall's configuration to let HTTP traffic through. 您有两种选择:敲除防火墙或编辑防火墙的配置以允许HTTP流量通过。 I opted to knock out the firewall as the simpler option (for me). 我选择将防火墙作为更简单的选项(对我而言)。

sudo service iptables stop sudo服务iptables停止

There is no real security risk in shutting down iptables because iptables , if active, merely duplicates the functionality of Amazon's firewall, which is using the Security Group to generate its configuration file. 关闭iptables没有真正的安全风险,因为iptables (如果处于活动状态)只会复制亚马逊防火墙的功能,后者正在使用安全组生成其配置文件。 We are assuming here that Amazon AWS doesn't misconfigure its firewalls - a very safe assumption. 我们假设亚马逊AWS没有错误配置其防火墙 - 这是一个非常安全的假设。

(3) Now, you can access the URL from your browser. (3)现在,您可以从浏览器访问该URL。

(4) The Microsoft Windows Servers also run their personal firewalls by default and you'll need to fix the Windows Server's personal firewall, too. (4)默认情况下,Microsoft Windows Server也运行其个人防火墙,您也需要修复Windows Server的个人防火墙。

Correction: by AWS default, AWS does not fire up server firewalls such iptables (Centos) or UAF (Ubuntu) when you are ordering the creation of new EC2 instances - That's why EC2 instances that are in the same VPC can ssh into each other and you can "see" the web server that you fired up from another EC2 instance in the same VPC. 更正:根据AWS默认情况下,当您订购新EC2实例的订单时,AWS不会启动服务器防火墙,如iptables(Centos)或UAF(Ubuntu) - 这就是为什么位于同一VPC中的EC2实例可以互相攻击的原因您可以“查看”从同一VPC中的另一个EC2实例启动的Web服务器。

Just make sure that your RESTful API is listening on all interfaces ie 0.0.0.0:portID 只需确保您的RESTful API正在侦听所有接口,即0.0.0.0:portID

The standard tcp sockets interface requires that you bind to a particular IP address when you send or listen. 标准的tcp套接字接口要求您在发送或侦听时绑定到特定的IP地址。 There are a couple of somewhat special addresses: localhost (which you're probably familiar with) which is 127.0.0.1. 有几个有点特殊的地址:localhost(你可能很熟悉),它是127.0.0.1。 There's also a special address, 0.0.0.0 or INADDR_ANY (internet protocol, special shorthand for ANY ADDRESS). 还有一个特殊地址,0.0.0.0或INADDR_ANY(互联网协议,任何地址的特殊简写)。 It's a way to listen on ANY or more commonly, ALL addresses on the host. 这是一种在主机上侦听任何或更常见的所有地址的方法。 This is a way to tell the kernel/stack that you're not interested in a particular IP address. 这是一种告诉内核/堆栈您对特定IP地址不感兴趣的方法。

So, when you're setting up a server that listens to "localhost" you're telling the service that you want to use the special reserved address that can only be reached by users of this host, and while it exists on every host, making a connection to localhost will only ever reach the host you're making the request from. 因此,当您设置一个侦听“localhost”的服务器时,您告诉服务您要使用该主机的用户只能访问的特殊保留地址,并且当它存在于每个主机上时,建立与localhost的连接只会到达您发出请求的主机。

When you want a service to be reachable everywhere (on a local host, on all interfaces, etc.) you can specify 0.0.0.0. 如果希望服务可以到达任何地方(在本地主机上,在所有接口上等),则可以指定0.0.0.0。

As you are getting connection refused (packets are being rejected) I bet it is iptables causing the problem. 当你得到连接被拒绝(数据包被拒绝)我打赌它是iptables导致问题。 Try to run 试着跑

iptables -I INPUT -p tcp --dport 80 -j ACCEPT iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 8888 -j ACCEPT iptables -I INPUT -p tcp --dport 8888 -j ACCEPT

and test the connection. 并测试连接。

You will also need to add those rules permanently which you can do by adding the above lines into ie. 您还需要永久添加这些规则,您可以通过将以上行添加到ie中来实现。 /etc/sysconfig/iptables if you are running Red Hat. / etc / sysconfig / iptables如果你正在运行Red Hat。

Apparently I was "binding to localhost" whereas I needed to bind to 0.0.0.0 to respond to port 80 for the all incoming TCP interfaces (?). 显然我是“绑定到localhost”而我需要绑定到0.0.0.0以响应所有传入TCP接口(?)的端口80。 This is a subtlety of TCP/IP that I don't fully understand yet, but it fixed the problem. 这是TCP / IP的一个微妙之处,我还没有完全理解,但它解决了这个问题。

Had to do the following: 不得不做以下事情:

1) Enable HTTP access on the instance config, it wasn't on by default only SSH 2) Tried to do nodejs server, so port was bound to 80 -> 3000 did the following commands to fix that 1)在实例配置上启用HTTP访问,默认情况下它没有打开只有SSH 2)尝试做nodejs服务器,所以端口绑定到80 - > 3000做了以下命令来修复

iptables -F
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo service iptables-persistent flush

Amazon support answered it and it worked instantly: 亚马逊的支持解决了它,它立即起作用:

I replicated the issue on my end on a test Ubuntu instance and was able to solve it. 我在测试Ubuntu实例上复制了我的问题,并且能够解决它。 The issue was that in order to run Tomcat on a port below 1024 in Ubuntu/Unix, the service needs root privileges which is generally not recommended as running a process on port 80 with root privileges is an unnecessary security risk. 问题是,为了在Ubuntu / Unix中在1024以下的端口上运行Tomcat,该服务需要root权限,通常不建议使用root权限在端口80上运行进程是不必要的安全风险。

What we recommend is to use a port redirection via iptables :- 我们建议通过iptables使用端口重定向: -

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080 iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

I hope the above information helps. 我希望以上信息有所帮助。

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

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