使用SSL(端口转发)访问Vagrant沙盒上的Apache

14

我已经建立了一个vagrant/virtualbox网络服务器作为开发沙盒,并在虚拟机中配置了apache的ssl(在默认的443端口上,使用自签名证书)。我已经使用curl在虚拟机上测试了页面。

curl -v -k https://mysite.mydomain.com/testSearch/results?postcode=WN8+0BA

看起来一切都很顺利,所以我很满意apache在虚拟机中的正确配置和工作。

然而,当我尝试通过https从我的主机浏览器访问虚拟机时,我无法这样做。

我已经添加了

config.vm.forward_port "https", 443, 8443

我正在尝试访问我的Vagrantfile中的URL。

https://mysite.mydomain.com:8443/testSearch/results?postcode=WN8+0BA

无法显示页面。我已经尝试了几个不同的浏览器:IE 给出一个毫无意义的“Internet Explorer 无法显示网页”的提示;Chrome 给出

SSL connection error
Unable to make a secure connection to the server. This may be a problem with the server or it may be requiring a client authentication certificate that you don't have.
Error 107 (net::ERR_SSL_PROTOCOL_ERROR): SSL protocol error.

火狐浏览器显示给我
An error occurred during a connection to mysite.mydomain.com:8443.
SSL received a record that exceeded the maximum permissible length.
(Error code: ssl_error_rx_record_too_long)

但是即使使用Firebug网络选项卡,我也无法获得更多的信息。

我在虚拟机apache的访问或错误日志中没有发现任何内容,因此我怀疑vagrant根本没有转发ssl。

  • VM客户操作系统:centos56x64
  • 主机:Windows 7 64位
  • JRuby:1.6.3(ruby-1.8.7-p330)(2011年07月07日965162f)(Java HotSpot(TM) 64-Bit Server VM 1.6.0_24)[Windows 7-amd64-java]
  • Vagrant:0.7.8
  • VirtualBox:4.0.12

如果有帮助,将不胜感激。

2个回答

24

1) 配置 Vagrantfile 文件

Vagrant::Config.run do |config|
    config.vm.box = "lucid32"
    config.vm.network "33.33.33.10"
    config.vm.forward_port "http", 80, 8080
end

2) 访问您的虚拟机"lucid32"

vagrant ssh

3) 在您的虚拟机内部,配置Apache“虚拟主机”:

<VirtualHost 33.33.33.10:80>
    ServerName        your-domain.dev
    DocumentRoot    /vagrant
    DirectoryIndex    index.php index.html index.htm

    <Directory /vagrant>
        AllowOverride All
        Allow from All
    </Directory>
</VirtualHost>

<VirtualHost 33.33.33.10:443>
    ServerName        your-domain.dev
    DocumentRoot    /vagrant
    DirectoryIndex    index.php index.html index.htm

    <Directory /vagrant>
        AllowOverride All
        Allow from All
    </Directory>

    SSLEngine on
    SSLCertificateFile /path/to/certicate/apache.pem
</VirtualHost>

4) 退出虚拟机并配置主机上的 "hosts" 文件:

33.33.33.10    your-domain.dev

6
当使用此解决方案时,每当您销毁Vagrant虚拟机时,都需要一遍又一遍地执行第2步和第3步。如果使用配置管理工具(如bash脚本、Chef或Puppet),将大大减少这种重复性的任务。 - Martin de Keijzer
2
对于谷歌员工,我必须同时指定 SSLCertificateFile.crt 文件和 SSLCertificateKeyFile.key 文件。 - Morgan Delaney

0
上面的答案要求您每次销毁盒子时都要重复步骤2和3。我建议您使用Chef来实现您的目标。请参阅以下示例:
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|

    config.vm.box       = "precise64"
    config.vm.box_url   = "http://files.vagrantup.com/precise64.box"

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

    config.vm.network "private_network", ip: "192.168.33.10"

    config.vm.provision :chef_solo do |chef|

        chef.cookbooks_path = "/path/to/your/cookbooks"

        # Install PHP
        chef.add_recipe "php"
        chef.add_recipe "php::module_mysql"

        # Setup Apache
        chef.add_recipe "apache2"
        chef.add_recipe "apache2::mod_php5"

        chef.json = { :apache => { :default_site_enabled => true } }

    end

end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接