2017-05-31 101 views
0

我是新來的後端開發。我正在使用vagrant和virtualbox創建一個基本的Web服務器。 Agter從VM運行服務器,當我嘗試從主機訪問時,出現上述錯誤。使用本地主機時ERR_CONNECTION_RESET

以下是無業遊民配置:

Vagrant.configure(2) do |config| 
    config.vm.box = "ubuntu/trusty32" 
    config.vm.network "forwarded_port", guest: 80, host: 8080 
end 

以下是在服務器文件中的main()代碼:

def main(): 
    try: 
     port = 8080 
     server = HTTPServer(('', port), webserverHandler) 
     print "Web server running on port %s" % port 
     server.serve_forever() 

    except KeyboardInterrupt: 
     print "^C entered. Shutting down the server..." 
     server.socket.close() 

讀了很多關於它之後,我嘗試了一些辦法讓它工作但無濟於事。 也telnet localhost 8080主機上給出了:

Trying 127.0.0.1... 
Connected to localhost. 
Escape character is '^]'. 
Connection closed by foreign host. 

運行流浪漢起來表明,它是轉發端口80(客戶)到8080(主機)。但在VM上運行sudo netstat -ntlp | grep LISTEN給出:

tcp  0  0 0.0.0.0:111    0.0.0.0:*    LISTEN  491/rpcbind  
tcp  0  0 0.0.0.0:22    0.0.0.0:*    LISTEN  685/sshd   
tcp  0  0 0.0.0.0:52166   0.0.0.0:*    LISTEN  649/rpc.statd 
tcp6  0  0 :::57101    :::*     LISTEN  649/rpc.statd 
tcp6  0  0 :::111     :::*     LISTEN  491/rpcbind  
tcp6  0  0 :::22     :::*     LISTEN  685/sshd 

運行curl -v 'http://localhost:8080'給出:

* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 8080 (#0) 
> GET/HTTP/1.1 
> Host: localhost:8080 
> User-Agent: curl/7.47.0 
> Accept: */* 
> 
* Recv failure: Connection reset by peer 
* Closing connection 0 
curl: (56) Recv failure: Connection reset by peer 

在VM上運行curl 'http://localhost:80'給出:

curl: (7) couldn't connect to host 

這是輸出當我運行vagrant up

==> default: Preparing network interfaces based on configuration... 
    default: Adapter 1: nat 
==> default: Forwarding ports... 
    default: 80 (guest) => 8080 (host) (adapter 1) 
    default: 22 (guest) => 2222 (host) (adapter 1) 
==> default: Booting VM... 
==> default: Waiting for machine to boot. This may take a few minutes... 
    default: SSH address: 127.0.0.1:2222 
    default: SSH username: vagrant 
    default: SSH auth method: private key 
==> default: Machine booted and ready! 
==> default: Checking for guest additions in VM... 
    default: The guest additions on this VM do not match the installed version of 
    default: VirtualBox! In most cases this is fine, but in rare cases it can 
    default: prevent things such as shared folders from working properly. If you see 
    default: shared folder errors, please make sure the guest additions within the 
    default: virtual machine match the version of VirtualBox you have installed on 
    default: your host and reload your VM. 
    default: 
    default: Guest Additions Version: 4.2.0 
    default: VirtualBox Version: 5.0 
==> default: Mounting shared folders... 
    default: /vagrant => /home/priyanshu/fullstack 
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` 
==> default: flag to force provisioning. Provisioners marked to run always will still run 

我對此完全陌生。讓我知道我在這裏做錯了什麼。

+0

你在虛擬機上沒有任何東西在80端口上運行,你需要啓動apache或nginx。 python服務器在8080端口上運行,你啓動它嗎? –

+0

我的不好。在vagrantfile中將訪客端口更改爲8080。它現在有效。謝謝! –

回答

1

你的Python服務器在端口8080運行,並沒有什麼運行端口80(爲netstat命令的輸出所示)

你的情況,你需要從你的Python服務器

更改客戶端的一個
config.vm.network "forwarded_port", guest: 8080, host: 8080