使用Vagrant为Nginx进行端口转发

时间:2014-09-29 23:11:01

标签: nginx vagrant

我遇到了一个问题: 我使用Windows 7并运行Vagrant Box(Lucid32),并在其中使用Nginx Server。

如果我跑

$ curl localhost:80 

我可以完美地看到Nginx的欢迎页面。

然而,在Windows中,端口2000(我已经做过的“端口”无法实现。

否则,如果我运行rails服务器,我可以在VM外部访问它。

我为nginx.conf做了这个基本配置:

server{
  listen 8080;
  server_name localhost;  
  access_log /usr/local/nginx/logs/access.log;
  access_log /usr/local/nginx/logs/error.log;
  location /{
    root /var/www;
    index index.html index.html;
  }

}

我的vagrantfile:

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "lucid32"

  config.vm.network :forwarded_port, guest: 3000, host: 3000    # rails
  #config.vm.network :forwarded_port, guest: 3306, host: 3306    # mysql
  config.vm.network :forwarded_port, guest: 2000, host: 80  # apache/nginx  
end

通过vagrant up命令输出:

Bringing machine 'default' up with 'virtualbox' provider...
[default] Clearing any previously set forwarded ports...
[default] Fixed port collision for 2000 => 80. Now on port 2200.
[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] -- 3000 => 3000 (adapter 1)
[default] -- 2000 => 8080 (adapter 1)
[default] -- 2000 => 2200 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.2.0
VirtualBox Version: 4.3
[default] Mounting shared folders...
[default] -- /vagrant

谢谢!

1 个答案:

答案 0 :(得分:2)

如果我正确读取所有内容,我认为您的端口号可能会颠倒过来。例如,如果您在客户操作系统(Linux)内的端口8080上运行nginx并希望它在主机操作系统(Windows)上显示为端口80,那么该行将如下所示:

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

但是,您发布的Vagrantfile和来自vagrant的日志似乎不匹配。 (在每种情况下都没有提到相同的端口。)