virtualbox+vagrant学习-3-Vagrant Share-2-HTTP Sharing

HTTP Sharing

Vagrant Share能够建立一个可公开访问的URL端点来访问在Vagrant环境中运行的HTTP服务器。这被称为“HTTP共享”,在使用Vagrant Share时默认启用。javascript

由于这种共享模式建立了一个可公开访问的URL,因此访问方不须要安装Vagrant来查看你的环境。html

这有许多有用的用例:你能够经过向internet公开你的Vagrant环境来测试webhook,你能够向客户、团队成员或经理等显示你的工做。java

 

1.Usage使用

为了使用HTTP共享,能够简单地运行vagrant share命令:web

userdeMBP:~ user$ vagrant share
Vagrant Share now defaults to using the `ngrok` driver.
The `classic` driver has been deprecated.

For more information about the `ngrok` driver, please
refer to the documentation:

  https://www.vagrantup.com/docs/share/

==> default: Detecting network information for machine...
The Vagrant environment you're attempting to share appears to
not be running. Please verify that it is running and try again.

这是由于virtualBox上的虚拟机挂起了,须要调用vagrant resume命令将其恢复,再运行vagrant share:服务器

userdeMBP:~ user$ vagrant share
Vagrant Share now defaults to using the `ngrok` driver.
The `classic` driver has been deprecated.

For more information about the `ngrok` driver, please
refer to the documentation:

  https://www.vagrantup.com/docs/share/

==> default: Detecting network information for machine...
    default: Local machine address: 127.0.0.1
    default:  
    default: Note: With the local address (127.0.0.1), Vagrant Share can only
    default: share any ports you have forwarded. Assign an IP or address to your
    default: machine to expose all TCP ports. Consult the documentation
    default: for your provider ('virtualbox') for more information.
    default:  
Vagrant was unable to detect an HTTP port for your machine.

Because your machine appears to have no static IP associated with
it, Vagrant scans through your forwarded ports, looking for one
that responds to an HTTP request. Vagrant couldn't find any
functioning HTTP port!

There are a few options to fix this error:

  1. Create a forwarded port pointing to your HTTP server inside
     your Vagrant machine.

  2. Specify an HTTP port manually with `--http` to this command.

  3. Assign a non-local address to your machine. This may or may
     not be possible depending on what provider you're using.

  4. Make sure that the HTTP server is up and running within
     your machine. Vagrant share won't start until it is
     reachable.

这是由于vagrant share探测本地发现没有打开一个http服务端口, 没法实现共享。session

所以打开了一个端口为80的服务器,并使用--http参数指明该端口:app

userdeMBP:~ user$ vagrant share --http 80
Vagrant Share now defaults to using the `ngrok` driver.
The `classic` driver has been deprecated.

For more information about the `ngrok` driver, please
refer to the documentation:

  https://www.vagrantup.com/docs/share/

==> default: Detecting network information for machine...
    default: Local machine address: 127.0.0.1
    default:  
    default: Note: With the local address (127.0.0.1), Vagrant Share can only
    default: share any ports you have forwarded. Assign an IP or address to your
    default: machine to expose all TCP ports. Consult the documentation
    default: for your provider ('virtualbox') for more information.
    default:  
    default: Local HTTP port: 80
    default: Local HTTPS port: disabled
    default: Port: 2222
==> default: Creating Vagrant Share session...
/opt/vagrant/embedded/gems/2.2.2/gems/vagrant-2.2.2/lib/vagrant/util/subprocess.rb:158:in `rescue in execute': No such file or directory - ngrok (Vagrant::Util::Subprocess::LaunchError)
    from /opt/vagrant/embedded/gems/2.2.2/gems/vagrant-2.2.2/lib/vagrant/util/subprocess.rb:151:in `execute'
    from /Users/user/.vagrant.d/gems/2.4.4/gems/vagrant-share-1.1.9/lib/vagrant-share/activate.rb:2451:in `block in start_ngrok_proxy'

这个问题是由于你没有安装成功ngrox或者是没有将其放在正确的位置,致使vagrant没有可以成功找到它框架

安装本博客ngrok反向代理学习-1-简介所写将其安装下来,而后要记得将下载下来的ngrox可执行文件放在你本地的vagrant可执行文件所在的位置:ssh

先查看vagrant所在的位置并打开:ide

userdeMBP:~ user$ which vagrant
/usr/local/bin/vagrant

userdeMBP:~ user$ open /usr/local/bin

而后将下载下来的ngrox可执行文件放到该bin目录下:

而后再运行就成功了:

userdeMBP:~ user$ vagrant share --http 80
Vagrant Share now defaults to using the `ngrok` driver.
The `classic` driver has been deprecated.

For more information about the `ngrok` driver, please
refer to the documentation:

  https://www.vagrantup.com/docs/share/

==> default: Detecting network information for machine...
    default: Local machine address: 127.0.0.1
    default:  
    default: Note: With the local address (127.0.0.1), Vagrant Share can only
    default: share any ports you have forwarded. Assign an IP or address to your
    default: machine to expose all TCP ports. Consult the documentation
    default: for your provider ('virtualbox') for more information.
    default:  
    default: Local HTTP port: 80
    default: Local HTTPS port: disabled
    default: Port: 2222
==> default: Creating Vagrant Share session...
==> default: HTTP URL: http://51399c1d.ngrok.io
==> default: 

 

 

而后打开http://localhost:4040网页:

而后用手机登陆http://51399c1d.ngrok.io网址,可见成功访问到了该80端口的网址:

而后http://localhost:4040页面出现详细的链接信息:

 

 

vagrant检测HTTP服务器在vagrant环境中运行的位置,并输出可用于访问此共享的端点。只要把这个URL(即上面的http://51399c1d.ngrok.io网址)给任何你想要分享它的人,他们就能访问你的vagrant环境!

若是vagrant在检测你的环境中的服务器端口时遇到困难,请使--http和/或--https标志来明确开启的端口。

该共享在vagrant share运行期间是可访问的。按Ctrl-C退出共享会话,即:

==> default: Creating Vagrant Share session...
==> default: HTTP URL: http://51399c1d.ngrok.io
==> default: 
^C==> default: Halting Vagrant share!

⚠️任何人均可以访问这个URL,因此若是你正在共享敏感信息,请当心。

 


2.Disabling禁用

若是你想禁用公共可访问端点的建立,请使用--disable-http标志运行vagrant share。这将使用其余可用方法之一(如ssh等)共享你的环境,而且不会建立URL端点。

即不会去检测是否有http的服务端口正在打开:

userdeMBP:~ user$ vagrant share --disable-http
Vagrant Share now defaults to using the `ngrok` driver.
The `classic` driver has been deprecated.

For more information about the `ngrok` driver, please
refer to the documentation:

  https://www.vagrantup.com/docs/share/

==> default: Detecting network information for machine...
    default: Local machine address: 127.0.0.1
    default:  
    default: Note: With the local address (127.0.0.1), Vagrant Share can only
    default: share any ports you have forwarded. Assign an IP or address to your
    default: machine to expose all TCP ports. Consult the documentation
    default: for your provider ('virtualbox') for more information.
    default:  
    default: Local HTTP port: disabled  //可见没有检测当前正打开的80端口
    default: Local HTTPS port: disabled //ssh在默认状况下就是禁用的 default: Port: 2222
==> default: Creating Vagrant Share session...

当看完ssh后在运行一遍这个命令看结果

3.Missing Assets

共享的web应用程序必须使用相对路径来加载任何本地资产,如图像、样式表、javascript。

开发中的web应用程序将被远程访问。这意味着若是你有任何硬编码的资产(图像、样式表等)的URLs如 <img src="http://127.0.0.1/header.png">,它们将不会为那些访问你的共享的人加载这些资产

大多数web框架或工具包都有设置或帮助程序来生成相对路径。例如,若是你是WordPress开发人员,Root Relative URLs插件将自动为你完成这项工做。

相对于资产的url一般是一种最佳实践,因此不管如何你都应该这样作!


4.HTTPS (SSL)

vagrant share还能够公开能够经过SSL访问的SSL端口。建立HTTPS共享须要一个非免费的ngrok账户。

在开发环境中,vagrant share默认状况下在端口443上查找任何SSL通讯。若是找不到,则默认禁用SSL。

能够使用--disable-https标志显式禁用HTTPS共享。

相关文章
相关标签/搜索