HTTP Sharing
Vagrant Share可以创建一个可公开访问的URL端点来访问在Vagrant环境中运行的HTTP服务器。这被称为“HTTP共享”,在使用Vagrant Share时默认启用。
因为这种共享模式创建了一个可公开访问的URL,所以访问方不需要安装Vagrant来查看你的环境。
这有许多有用的用例:你可以通过向internet公开你的Vagrant环境来测试webhook,你可以向客户、团队成员或经理等显示你的工作。
1.Usage使用
为了使用HTTP共享,可以简单地运行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...
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服务端口, 无法实现共享。
因此打开了一个端口为80的服务器,并使用--http参数指明该端口:
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可执行文件所在的位置:
先查看vagrant所在的位置并打开:
userdeMBP:~ user$ which vagrant
/usr/local/bin/vagrantuserdeMBP:~ 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://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如 ,它们将不会为那些访问你的共享的人加载这些资产
大多数web框架或工具包都有设置或帮助程序来生成相对路径。例如,如果你是WordPress开发人员,Root Relative URLs插件将自动为你完成这项工作。
相对于资产的url通常是一种最佳实践,所以无论如何你都应该这样做!
4.HTTPS (SSL)
vagrant share还可以公开可以通过SSL访问的SSL端口。创建HTTPS共享需要一个非免费的ngrok帐户。
在开发环境中,vagrant share默认情况下在端口443上查找任何SSL通信。如果找不到,则默认禁用SSL。
可以使用--disable-https标志显式禁用HTTPS共享。