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共享。

相關文章
相關標籤/搜索