Domanda

I have Vagrant/VirtualBox running an Ubuntu 12.04 LTS OS. I have configured Vagrant to forward the guest port 8000 to my host port 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! 

When the virtual machine starts up, I start a Django dev server on port 8000.

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

Okay great, I can put it in the background and I can even curl localhost:8000 and get some output from the server

<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>

But when I try to hit the server from my host machine with a Firefox/Chrome/Telnet I'm getting Connection Reset/Connection Lost/ERR_CONNECTION_RESET etc.

First I thought it may be some iptables thing, but it turns out Ubuntu has default allow everything. I also turned off the firewall on my host machine. How can I get to the bottom of this?

È stato utile?

Soluzione

I had Django listening on 127.0.0.1:8000 (default)

As explained in Mitchell's answer here: Vagrant's port forwarding not working I should have been listening on 0.0.0.0. Here is a quote of his answer:

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.

If you're using Django, you want to start the dev server like this: ./manage.py runserver 0.0.0.0:8000

Altri suggerimenti

1). Project must be run as python manage.py runserver 0.0.0.0:8000.
if this is not fix your problem then

2). Open your Vagrantfile and add
config.vm.network "forwarded_port", guest: 8000, host: 8001 this after
# config.vm.network "forwarded_port", guest: 80, host: 8080 line

This work in my case ;)

Note: This forward port info provided when run vagrant up
....
==> default: Forwarding ports...

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

Another solution is to use ssh tunneling with vagrant. Open up a terminal and cd into the directory where your Vagrantfile is. Assuming your vm is up and running (vagrant up) execute the following command.

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

Afterwards forward guest's port 8000 to host.

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

Now just open your browser and check 127.0.0.1:8000.

Note that the previous command will also open a shell on ssh server. If you want to open ssh tunnel without a shell on ssh server add -N

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

Update: Oct 4 2019

A simpler solution to forward port

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

Tested on Vagrant 2.0.2, but may work for older versions as well.

Adding to @GrvTyagi's answer.

I am using ubuntu : "hashicorp/bionic64"

My Vagrant file

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

For some reaseon default port (80) suggested by offical documentaion is not working. so below config will not work:

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

TEst your port forwarding by running a simple server

python3 -m http.server 8081

You should able to connect with it from the host using the command

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

Once successful,force run your application on host 0.0.0.0 and port 8081 running on localhost will not work.

yourappserver --bindhost 0.0.0.0 -bindport 8081

you should able to connect with it from the host by

http://0.0.0.0:8080
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top