32

我有运行 Ubuntu 12.04 LTS 操作系统的 Vagrant/VirtualBox。我已将 Vagrant 配置为将访客端口 8000 转发到我的主机端口 8888。

[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] -- 8000 => 8888 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use! 

当虚拟机启动时,我在端口 8000 上启动了一个 Django 开发服务器。

Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

好的,我可以把它放在后台,我什curl localhost:8000至可以从服务器获取一些输出

<div id="explanation">
  <p>
    You're seeing this message because you have <code>DEBUG = True</code> in your
    Django settings file and you haven't configured any URLs. Get to work!
  </p>
</div>

但是当我尝试使用 Firefox/Chrome/Telnet 从我的主机上访问服务器时,我得到了 Connection Reset/Connection Lost/ERR_CONNECTION_RESET 等。

首先我认为这可能是一些 iptables 的东西,但事实证明 Ubuntu 默认允许一切。我还关闭了主机上的防火墙。我怎样才能找到这件事的底部?

4

4 回答 4

86

我让 Django 在 127.0.0.1:8000 上监听(默认)

正如 Mitchell 在此处的回答中所解释的那样:Vagrant 的端口转发不起作用我应该一直在监听 0.0.0.0。这里引用他的回答:

我想补充一点,这通常是由 VM 中的服务器引起的,因为它绑定到 127.0.0.1,即环回。您需要确保服务器绑定到 0.0.0.0,以便所有接口都可以访问它。

如果您使用的是 Django,您希望像这样启动开发服务器:./manage.py runserver 0.0.0.0:8000

于 2013-08-10T00:35:47.150 回答
4

1)。项目必须以python manage.py runserver 0.0.0.0:8000.
如果这不能解决您的问题,那么

2)。打开您的 Vagrantfile 并在行后
config.vm.network "forwarded_port", guest: 8000, host: 8001添加
# config.vm.network "forwarded_port", guest: 80, host: 8080

在我的情况下这项工作;)

注意:运行时提供的此转发端口信息vagrant up
....
==> default: Forwarding ports...

default: 8000 => 8001 (adapter 1)
default: 22 => 2222 (adapter 1)

于 2015-07-04T09:21:07.493 回答
2

另一种解决方案是将ssh 隧道与 vagrant 一起使用。打开终端并cd进入您所在的目录Vagrantfile。假设您的 vm 已启动并正在运行 ( vagrant up),请执行以下命令。

# save vagrant ssh config to a file named `vagrant-ssh-config`
vagrant ssh-config > vagrant-ssh-config

然后将客人的8000端口转发给主机。

ssh -L 8000:127.0.0.1:8000 -F vagrant-ssh-config default

现在只需打开浏览器并检查127.0.0.1:8000.

请注意,前面的命令也会在 ssh 服务器上打开一个 shell。如果要在 ssh 服务器上打开没有 shell 的 ssh 隧道,请添加-N

ssh -N -L 8000:127.0.0.1:8000 -F vagrant-ssh-config default

更新:2019 年 10 月 4 日

转发端口的更简单的解决方案

vagrant ssh -- -N -L 8000:127.0.0.1:8000

在 Vagrant 2.0.2 上测试,但也可能适用于旧版本。

于 2017-05-14T09:45:09.693 回答
0

添加到@GrvTyagi 的答案。

我正在使用 ubuntu:“hashicorp/bionic64”

我的流浪档案

Vagrant.configure(2) do |config|
  config.vm.box = "hashicorp/bionic64"
  #Ubuntu 18.04
  config.vm.box_check_update = false
  config.vm.network "forwarded_port", guest: 8081, host: 8080
    config.vm.provider "virtualbox" do |vb|
      vb.gui = false
      vb.cpus = 2
      vb.memory = "4096"
    end
  
  config.vm.provision "shell", inline: <<-SHELL
  # your script to execute while provision the server goes here
  SHELL

end

由于某些原因,官方文档建议的默认端口(80)不起作用。所以下面的配置将不起作用:

config.vm.network "forwarded_port", guest: 80, host: 8080

通过运行一个简单的服务器来测试你的端口转发

python3 -m http.server 8081

您应该能够使用命令从主机连接它

curl http://localhost:8080/ --trace-ascii dump.txt

成功后,强制在主机 0.0.0.0 上运行您的应用程序,在 localhost 上运行的端口 8081 将不起作用。

yourappserver --bindhost 0.0.0.0 -bindport 8081

您应该能够通过以下方式从主机连接它

http://0.0.0.0:8080
于 2022-01-30T19:58:16.270 回答