流浪汉的港口转发不工作

vagrant入门指南的末尾我遇到了一个小问题。我正在开发一个运行 Apache2的 CentOS 基箱(通过 Puppet 进行供应)。我在 Vagrantfile中使用以下代码行为 Web 请求设置了端口转发:

 config.vm.forward_port "web", 80, 4567

但当我向那个港口发出请求时,他们失败了。Safari 报告的错误是‘ Safari 无法打开页面“ http://localhost:4567/”,因为服务器意外断开了连接。'

我做了一个 vagrant reload,在滚动条中看到“[ default ]-web: 80 = > 4567(Adapter1)”,那么我应该从哪里开始进行故障排除呢?谢谢。

101246 次浏览

I'll make this an actual answer instead of just more comments.

First thing: try curl 'http://localhost:80' from within the VM. If that doesn't work, then it's definitely not the port forwarding.

Next: try curl -v 'http://localhost:4567/' from your host machine. Curl might give you a better error message than Safari.

I'd check that there are no firewalls set up restricting access to port 80. The default Vagrant VM (Ubuntu) doesn't come with a firewall set up, but you said you're using something else, so it might be worth it to check.

If that's not it, try making something other than Apache listed on port 80. Python ships with a simple HTTP server you can use -- go to the folder with index.html and run sudo python -m SimpleHTTPServer 80, then try hitting that with curl from both boxes. If that works, then it's probably an Apache configuration issue. I don't have enough experience with Apache to help if that's the case (I use nginx).

I wanted to add an additional note that often this is caused by the server within the VM because it binds to 127.0.0.1, which is loopback. You'll want to make sure that the server is bound to 0.0.0.0 so that all interfaces can access it.

Some built-in app servers such as Django's development servers and some Ruby servers default to 127.0.0.1 by default so this is something to watch out for.

Other than that, what Steve said holds true: Make sure it works from within the VM and try some other simple servers to try and figure out if it is a configuration problem.

I want to add another note like Mitchell as well. if my case I forward it to 6789 from 80

$ curl -v http://localhost:6789

And I got

<HTML>
<HEAD><TITLE>Redirection</TITLE></HEAD>
<BODY><H1>Redirect</H1></BODY>

Then, I used the IP address instead, it got the correct html message.

I had the same problem on CentOS 6.3 w/ NGINX and found the answer to be in the iptables on the vagrant box.

From bash on the vagrant box, follow these steps:

First list current iptable rules

iptables -L -v

Then flush current rules:

iptables -F

Allow SSH connections on tcp port 22

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

Set default policies for INPUT, FORWARD and OUTPUT chains

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

Set access for localhost

iptables -A INPUT -i lo -j ACCEPT

Accept packets belonging to established and related connections

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Save settings

/sbin/service iptables save

List modified rules

iptables -L -v

Curl localhost:[port#] or hit it in your browser from outside vagrant

More info on CentOS iptable configs found here:

http://wiki.centos.org/HowTos/Network/IPTables

Good luck.

A better solution for me is disabling the firewall

service iptables stop
chkconfig iptables off