apache不接受来自localhost外部的传入连接

时间:2012-05-23 23:25:29

标签: linux apache webserver centos

我在rackspace上启动了一个CentOS服务器并执行yum install httpd'd。然后是services httpd start。所以,只是准系统。

我可以通过ssh(22)远程访问其IP地址没问题,所以DNS或任何东西都没有问题(我认为......),但当我尝试连接端口80时(通过浏览器或其他东西) )我拒绝连接。

然而,从localhost,我可以使用telnet(80),甚至是lynx本身,并且没有问题。从外面(我的房子,我的学校,当地的咖啡店等),telnet连接22,但不是80.

我使用netstat -tulpn(< - 我不会撒谎,我不理解-tulpn部分,但这是互联网告诉我要做的......)并且看到

tcp    0    0 :::80     :::*    LISTEN    -                   
我相信我应该这样做。 httpd.confListen 80

我有很多次services httpd restart

老实说,我不知道该怎么做。机架空间没有办法在传入的端口80请求上有防火墙。我觉得我错过了一些愚蠢的东西,但是我现在已经两次启动了一个准系统服务器并且已经做了绝对最小化以实现这个功能,以为我用修修补补了一些东西,但都没有奏效。

非常感谢任何帮助! (对于长篇大论的帖子感到抱歉......)

修改 我被要求发布iptables -L的输出。所以这就是:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

11 个答案:

答案 0 :(得分:108)

如果还没有解决的话。你的iptables说:

  

州相关,已建立

这意味着它只允许传递已经建立的连接......这是由您建立的,而不是由远程机器建立的。然后,您可以在下一个规则中看到此例外情况:

state NEW tcp dpt:ssh

仅对ssh计数,因此您应该为http添加类似的规则/行,您可以这样做:

state NEW tcp dpt:80

你可以这样做:

sudo iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

(在这种情况下,我选择在第四行添加新规则)

请记住,在编辑文件后,您应该将其保存为:

sudo /etc/init.d/iptables save

答案 1 :(得分:15)

现在,CentOS 7默认使用firewalld。但所有答案都集中在iptables上。所以我想添加一个与firewalld相关的答案。

因为firewalld是一个"包装"对于iptables,使用antonio-fornie的答案似乎仍然有效,但我无法保存"新规则。因此,一旦重启防火墙,我就无法连接到我的apache服务器。幸运的是,使用firewalld命令进行等效更改实际上要简单得多。首先检查firewalld是否正在运行:

firewall-cmd --state

如果它正在运行,响应只会是一行,表示"正在运行"。

在公共区域暂时允许http(端口80)连接:

sudo firewall-cmd --zone=public --add-service=http

以上不会"保存",下次重新启动firewalld服务时,它将返回默认规则。您应该使用此临时规则进行测试,并确保它在继续之前解决您的连接问题。

永久允许公共区域的http连接:

sudo firewall-cmd --zone=public --permanent --add-service=http

如果你做"永久性"命令没有做"临时"您也需要重新启动firewalld以获取新的默认规则(对于非CentOS系统,这可能会有所不同):

 sudo systemctl restart firewalld.service

如果这还没有解决您的连接问题,可能是因为您的界面不在"公共区域"。以下链接是了解firewalld的重要资源。它详细介绍了如何检查,分配和配置区域:https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-using-firewalld-on-centos-7

答案 2 :(得分:5)

SELinux默认阻止Apache(以及所有Apache模块)进行远程连接。

# setsebool -P httpd_can_network_connect=1

答案 3 :(得分:3)

尝试使用iptables.config表中的以下设置

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

运行以下命令重启iptable服务

service iptables restart

将httpd.config文件更改为

Listen 192.170.2.1:80

重新启动apache。

立即尝试。

答案 4 :(得分:2)

如果您使用的是RHEL / CentOS 7(OP不是,但我认为我会为我的案例分享解决方案),那么您将需要使用firewalld而不是其他答案中提到的iptables服务。

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

然后检查它是否正在运行:

firewall-cmd --permanent --zone=public --list-all

应在80/tcp

下列出ports

答案 5 :(得分:1)

在apache配置文件(httpd.conf,apache2.conf,listen.conf,...)中搜索LISTEN指令,如果看到localhost或127.0.0.1,则需要使用public ip覆盖。

答案 6 :(得分:1)

尝试禁用iptables:service iptables stop

如果此方法有效,请启用防火墙规则的TCP端口80: 从root运行system-config-selinux,并在防火墙上启用TCP端口80(HTTP)。

答案 7 :(得分:1)

这会奏效: - 对于REDHAT 使用:cat“/ etc / sysconfig / iptables”

iptables -I  RH-Firewall-1-INPUT -s 192.168.1.3  -p tcp -m tcp --dport 80 -j ACCEPT

接着是

sudo /etc/init.d/iptables save

答案 8 :(得分:1)

这对我们来说可以从外部访问apache:

sudo iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
sudo service iptables restart

答案 9 :(得分:0)

将apache设置为列表到特定的接口和端口,如下所示:

Listen 192.170.2.1:80

同时检查可能干扰主机的Iptables和TCP Wrappers条目以及访问该端口的外部主机

Binding Docs For Apache

答案 10 :(得分:-1)

禁用SELinux

$ sudo setenforce 0
相关问题