使用 Laravel Homestead 将 Vagrant 端口转发 80 到 8000
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24492901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vagrant port forwarding 80 to 8000 with Laravel Homestead
提问by Tyler
My Problem:
我的问题:
I can only access my sites through port 8000, but not 80, which makes me think it is not redirecting 80 to 8000 as it says it should be. I want to simply type local.kujif.com into my browser and it loads the site, which I read was port 80 by default. I am using curl to check it and it returns:
我只能通过端口 8000 访问我的网站,而不是 80,这让我认为它没有像它所说的那样将 80 重定向到 8000。我想简单地在我的浏览器中输入 local.kujif.com 并加载该站点,我读取的默认端口为 80。我正在使用 curl 来检查它并返回:
curl 'http://local.kujif.com'
curl: (7) Failed connect to local.kujif.com:80; No error
However if I add :8000 to the url then it works; it returns my index.php which simply prints 'test':
但是,如果我将 :8000 添加到 url 则它可以工作;它返回我的 index.php,它只是打印“test”:
curl 'http://local.kujif.com:8000'
test
My Details:
我的细节:
I am using Laravel Homestead and Vagrant with Oracle VM VirtualBox.
我正在将 Laravel Homestead 和 Vagrant 与 Oracle VM VirtualBox 一起使用。
In the Homestead.rb it has the port forwarding. I haven't edited it at all:
在 Homestead.rb 它有端口转发。我根本没有编辑它:
config.vm.network "forwarded_port", guest: 80, host: 8000
config.vm.network "forwarded_port", guest: 3306, host: 33060
config.vm.network "forwarded_port", guest: 5432, host: 54320
I also have Microsoft IIS installed for my work stuff. I obviously stop that service whenever I need vagrant to use the localhost.
我还为我的工作安装了 Microsoft IIS。每当我需要流浪者使用本地主机时,我显然都会停止该服务。
"vagrant up" shows:
“流浪者”显示:
My Homestead.yaml file:
我的 Homestead.yaml 文件:
---
ip: "192.168.10.10"
memory: 2048
cpus: 1
authorize: /Users/Tyler/.ssh/id_rsa.pub
keys:
- /Users/Tyler/.ssh/id_rsa
folders:
- map: C:\DEV\Linux
to: /var/www/
sites:
- map: homestead.app
to: /home/vagrant/Code/Laravel/public
- map: local.kujif.com
to: /var/www/kujif
variables:
- key: APP_ENV
value: local
回答by noeldiaz
You should continue to use ports above 1024 since they are non-privileged ports, BUT if you do want you can run as port 80 on the Homestead VM, as long as you don't have anything holding on to that port on the host machine. Just tried it and it worked, with a few gotchas. First, you change that line in the .rb file from:
您应该继续使用 1024 以上的端口,因为它们是非特权端口,但是如果您确实需要,您可以在 Homestead VM 上作为端口 80 运行,只要您没有任何东西占用主机上的该端口. 刚刚尝试过,它奏效了,但有一些问题。首先,您将 .rb 文件中的该行更改为:
config.vm.network "forwarded_port", guest: 80, host: 8000
to
config.vm.network "forwarded_port", guest: 80, host: 80
config.vm.network“forwarded_port”,来宾:80,主机:8000
到
config.vm.network“forwarded_port”,来宾:80,主机:80
When you fire your VM up after saving you will get a warning from vagrant:
保存后启动 VM 时,会收到 vagrant 的警告:
==> default: You are trying to forward to privileged ports (ports <= 1024). Most
==> default: operating systems restrict this to only privileged process (typically
==> default: processes running as an administrative user). This is a warning in case
==> default: the port forwarding doesn't work. If any problems occur, please try a
==> default: port higher than 1024.
==> default: Forwarding ports...
default: 80 => 80 (adapter 1)
==> 默认值:您正在尝试转发到特权端口(端口 <= 1024)。最多
==> 默认值:操作系统将此限制为仅特权进程(通常
==> 默认值:以管理用户身份运行的进程)。这是一个警告,以防万一
==> 默认值:端口转发不起作用。如果出现任何问题,请尝试
==> 默认值:高于 1024 的端口。
==> 默认值:转发端口...
default: 80 => 80 (adapter 1)
But it worked for me. Now, to actually get to the VM I had to use it's private IP instead of the localhost name:
但它对我有用。现在,要真正访问 VM,我必须使用它的私有 IP 而不是 localhost 名称:
http://192.168.10.10/
But sure enough my site was there and everything was working. If you decide to keep it that was you can add that IP address to your hosts file to give it a nice short name.
但果然我的网站在那里,一切正常。如果您决定保留原来的 IP 地址,您可以将该 IP 地址添加到您的主机文件中,以给它一个漂亮的短名称。
Hope this helps.
希望这可以帮助。
回答by Wairowe
I see there is an accepted answer, but this alternative may also help someone. If I understand correctly you really dislike the port "8000"!
我看到有一个可接受的答案,但这种替代方法也可能对某人有所帮助。如果我理解正确,您真的不喜欢端口“8000”!
Have you tried setting a private network?
您是否尝试过设置专用网络?
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
/*other config stuff here */
config.vm.network :private_network, ip: "192.168.33.22"
This way you can simply use that IP address, or edit you hosts file to map the local domain to that IP.
这样您就可以简单地使用该 IP 地址,或编辑您的主机文件以将本地域映射到该 IP。
Take a look at the Vagrant docs:Vagrant Private Networks
查看 Vagrant 文档:Vagrant Private Networks
BTW, You shouldn't need to shutdown your IIS local server as that is running on a totally different IP range. I have Apache running locally while also accessing the VM server. This allows you to use tools like composer (to pull in laravel) on your local if needed.
顺便说一句,您不需要关闭 IIS 本地服务器,因为它在完全不同的 IP 范围内运行。我在本地运行 Apache,同时还访问 VM 服务器。这允许您在需要时在本地使用像 composer(拉入 laravel)这样的工具。
回答by Kryten
I'm not sure what the confusion is - this is the way it's supposed to work.
我不确定混淆是什么 - 这是它应该工作的方式。
The web server on the VM listens on port 80. Vagrant/VirtualBox forwards that port from 80 (on the VM) to 8000 (on localhost) so that you can access the site at http://localhost:8000
.
VM 上的 Web 服务器侦听端口 80。Vagrant/VirtualBox 将该端口从 80(在 VM 上)转发到 8000(在 localhost),以便您可以访问该站点http://localhost:8000
。
Port 80 on the VM's domain name is not going to be available - that domain name probably resolves to localhost
.
VM 域名上的端口 80 将不可用 - 该域名可能解析为localhost
.
Try the following: dig local.kujif.com
(or nslookup
or even ping
- I don't know what tools are available on Windows) to find out what IP address that name is resolving to. You will probably find that it's 127.0.0.1
(localhost).
尝试以下方法:dig local.kujif.com
(或者nslookup
甚至是ping
-我不知道是什么工具都可以在Windows上),找出IP地址名称解析为。您可能会发现它是127.0.0.1
(localhost)。
You could try using the IP address set in the homestead file instead: http://192.168.10.10/
- this mightwork, but it will depend on how networking is configured in the VM.
您可以尝试使用在 homestead 文件中设置的 IP 地址:http://192.168.10.10/
- 这可能有效,但这取决于 VM 中的网络配置方式。
Ideally, you need to set networking to "bridged" in the VM - this will make the VM look (to your network) like any other device on the network. Other networking options in the VM (sorry, I'm not familiar with the options in VirtualBox) will set the VM up with its own network that is not accessible outside the VM - this is why port forwarding is used to expose network services on the VM.
理想情况下,您需要在虚拟机中将网络设置为“桥接” - 这将使虚拟机看起来(对于您的网络)与网络上的任何其他设备一样。VM 中的其他网络选项(抱歉,我不熟悉 VirtualBox 中的选项)将使用自己的网络设置 VM,该网络在 VM 外部无法访问 - 这就是端口转发用于公开网络服务的原因虚拟机。