vagrant port forwarding doesn't work: Connection reset by peer

扶醉桌前 提交于 2020-05-15 07:28:38

问题


ssh port forwarding (which is enabled by default) works fine, but my custom port forwardings don't.

I took the config below from the official docs, but it doesn't work :(

host machine runs Ubuntu 17.04

Vagrantfile is:

Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu/xenial64"
    # though, I've also tried ubunty/zesty64 and centos/7 - with same results
    config.vm.provision :shell, path: "vagrant/provision.sh"
    config.vm.network "forwarded_port", guest: 3000, host: 3001
end

http server is node.js, listening port 3000 at guest machine

when I run vagrant up I see that there are 2 port forwardings:

==> default: Forwarding ports...
    default: 3000 (guest) => 3001 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)

I successfully connect to guest via ssh on port 2222, thus the forwarding 22 (guest) => 2222 (host) works fine.

Accessing port 3000 from the guest machine (via ssh) also works fine:
(the following is a quote from ssh console when connected to guest machine)

ubuntu@ubuntu-xenial:~$ curl -v http://localhost:3000/
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Date: Fri, 14 Jul 2017 12:34:29 GMT
< Connection: keep-alive
< Content-Length: 12
< 
Hello World
* Connection #0 to host localhost left intact

But requesting port 3001 directly from host machine fails:
(the following is a quote from a regular console (not ssh) on local host machine)

$ curl -v http://127.0.0.1:3001/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 3001 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:3001
> User-Agent: curl/7.52.1
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Curl_http_done: called premature == 1
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

I also found out this has nothing to do with firewall, 'cause it's disabled (on both machines):

ubuntu@ubuntu-xenial:~$ sudo ufw status
Status: inactive

I also noticed that all other ports on both machines return the same:

ubuntu@ubuntu-xenial:~$ curl http://127.0.0.1:3003/
curl: (7) Failed to connect to 127.0.0.1 port 3003: Connection refused

— which simply means that those ports are closed on respective machine, and that is completely OK.

And only the port 3001 on host machine returns Connection reset by peer:

$ curl http://127.0.0.1:3001/
curl: (56) Recv failure: Connection reset by peer

— that means that something's wrong in network settings. But what exactly?

please, help me to set up port forwardings correctly!

UPDATE 1

Maybe this will be important: when upping vagrant, the following is printed in console:

==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 3000 (guest) => 3001 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)

Maybe the reason is that Vagrant for some reason forwards ports for the wrong adapter? Maybe it should forward for hostonly, and it forwards for nat? I don't understand it, it's just an assumption.


回答1:


Finally, I was able to fix it!

The reason wasn't a misconfiguration of port forwarding, neither some settings of Vagrant, nor even VirtualBox, nor Guest, nor Host OS.

The reason was I've incorrectly set up Node.JS' port listening.

What I've done (incorrect): I've set it up to listen port 3000 on guest machine. I thought that guest machine's local IP, available from inside itself, is, as usual, 127.0.0.1. Below is my Node.JS server code:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
    res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

What I had to do (correct): set it up to listen that same port, but on other IP: 0.0.0.0 — this is the correct local IP of guest machine, that is available to it itself internally, when using together with port forwarding.

Thus, my Vagrantfile is correct; here is what I've fixed in my Node.JS server:

const hostname = '0.0.0.0';
const port = 3000;
...
server.listen(port, hostname, () => {

P. S. I apologize for the inconvenience I caused by this question. Thank you to everyone who helped me! At first, when I found the solutuion, I was going to delete this question, but decided that it'd be better if I answer it myself instead, so that others like me can save their and other people's time spent on fixing such cases in future, so I decided to keep it.




回答2:


What you are trying to do would not work. The below config means that

config.vm.network "forwarded_port", guest: 3000, host: 3001

Forward the port 3000 on the vagrant box to port 3001 on my localhost, so the port 3000 on the vagrant machine can be accessed via port 3001 of your localhost.

If you want to directly access the 3000 on the vagrant machine, you have to create a private network to attach an IP to the vagrant machine, and access the port 3001 via this IP. As example

config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "forwarded_port", guest: 3000, host: 3001

Now you will be able to access the port 3000 by curl -v http://192.168.33.10:3000/



来源:https://stackoverflow.com/questions/45103449/vagrant-port-forwarding-doesnt-work-connection-reset-by-peer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!