端口转发在 Vagrant 中不起作用

Port forwarding not working in Vagrant

我已经安装了启动和 运行 vagrant 所需的所有工具。我可以通过 putty 从 windows host 登录 vm guest。但是当我尝试使用我在端口转发中指定的端口从 host windows os 访问 localhost 时,我无法访问那个

我会把它作为一个答案,因为它比写评论更清楚:

  • 从你展示的内容来看,你并不清楚你是否安装了 Apache、nginx 或其他网络服务器,所以如果没有安装,你会得到预期的行为。

  • 从虚拟机,去 运行 sudo netstat -ant(你需要 sudo 否则它无法检测进程的 pids 运行 root 权限),你应该有一些看起来像

    vagrant@precise32:/etc/init.d$ sudo netstat -nltp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      512/rpcbind
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1827/apache2
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      652/sshd
    tcp        0      0 0.0.0.0:58397           0.0.0.0:*               LISTEN      539/rpc.statd
    tcp6       0      0 :::111                  :::*                    LISTEN      512/rpcbind
    tcp6       0      0 :::22                   :::*                    LISTEN      652/sshd
    tcp6       0      0 :::54908                :::*                    LISTEN      539/rpc.statd
    

重要的部分是 0 0.0.0.0:80 所以我在端口 80 上有一个进程 运行ning,在我的例子中它是 apache2

如果你在端口 80 上没有看到 运行ning 的东西,你需要安装,运行 sudo apt-get install apache2 它会 install/start apache2 服务,你可以继续

  • 现在检查您的虚拟机页面上是否有 returns 内容,您可以 运行 curl 或任何您喜欢的内容

    vagrant@precise32:/etc/init.d$ curl localhost
    <html><body><h1>It works!</h1>
    <p>This is the default web page for this server.</p>
    <p>The web server software is running but no content has been added, yet.</p>
    </body></html>
    

所以这里很好,我的页面显示。

现在当我去主机上做同样的事情时,我可以看到相同的页面

fhenri@machine:~/project/examples/vagrant/ssh$ curl http://localhost:8080
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
fhenri@machine:~/project/examples/vagrant/ssh$ curl http://127.0.0.1:8080
<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
fhenri@machine:~/project/examples/vagrant/ssh$