Tôi đã cài đặt Vagrant (1.2.2) VM chạy VistualBox thành : private_network và tôi đã khởi động máy chủ Sinatra trên đó. Tuy nhiên tôi không thể kết nối với cá thể Sinatra đó. Tuy nhiên VM chạy và phản hồi lại ping.Vagrant từ chối kết nối trong chế độ private_network
Đây là Vagrantfile tôi
Vagrant.configure("2") do |config|
config.vm.box = "precise64"
config.vm.network :private_network, ip: "192.168.33.10"
end
Vì vậy, tôi bắt đầu Vagrant VM và ssh vào nó
prodserv$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
prodserv$ vagrant ssh
Welcome to Ubuntu 12.04.2 LTS (GNU/Linux 3.2.0-23-generic x86_64)
* Documentation: https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Thu May 23 14:01:05 2013 from 10.0.2.2
Vì vậy, đến đây tất cả là tốt và dandy. Một ping để VM sẽ làm việc tốt (Tôi cũng đã kiểm tra rằng đây thực sự là ip máy ảo. Vì vậy, ping mà không lang thang lên sẽ dẫn đến mất gói)
prodserv$ ping 192.168.33.10
PING 192.168.33.10 (192.168.33.10): 56 data bytes
64 bytes from 192.168.33.10: icmp_seq=0 ttl=64 time=0.543 ms
64 bytes from 192.168.33.10: icmp_seq=1 ttl=64 time=0.328 ms
tuyệt vời! Bây giờ tôi bắt đầu máy chủ trên VM
[email protected]:~$ sudo ruby /vagrant/server.rb
== Sinatra/1.4.2 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on localhost:4567, CTRL+C to stop
này là tương ứng server.rb
require 'rubygems'
require 'sinatra'
get '/' do
puts "WOW!"
'Hello, world!'
end
nếu tôi cuộn tròn bây giờ từ khách VM để Sinatra tất cả mọi thứ hoạt động tốt và " Chào thế giới!" sẽ được trả lại.
[email protected]:~$ curl 'http://localhost:4567'
Hello, [email protected]:~$
#and the Sintra/Ruby process gets me this
WOW!
127.0.0.1 - - [23/May/2013 16:06:36] "GET/HTTP/1.1" 200 13 0.0026
Tuy nhiên nếu tôi cố gắng để curl từ máy chủ kết nối bị từ chối.
prodserv$ curl -v 'http://192.168.33.10:4567'
* About to connect() to 192.168.33.10 port 4567 (#0)
* Trying 192.168.33.10...
* Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host
Vậy có chuyện gì nữa?
bạn nói đúng, đó không phải là câu hỏi lập trình. – robkuz
Bạn cần bật cổng chuyển tiếp cho 4567 trong Vagrantfile. Theo mặc định, cổng duy nhất chuyển tiếp đến vm chạy là 22 cho ssh. –