首页 > 解决方案 > 如何让端口转发与 gatsby 和 vagrant 或 virtualbox 一起使用

问题描述

当使用在 Vagrant(通过 Virtualbox)上运行的Gatsby时,我无法让端口转发工作。

Gatsby 默认在 localhost 端口 8000 上运行。

我的 Vagrantfile 似乎使用以下内容转发了正确的端口:

config.vm.network "forwarded_port", guest: 8000, host: 8000

yarn develop当我在Vagrant 框中使用

info bootstrap finished - 7.354 s

 DONE  Compiled successfully in 9091ms                                                                                                                                                                                                

You can now view foo-bar-org in the browser.

  http://localhost:8000/

我可以curl http://localhost:8000在来宾盒子上运行以验证它确实在本地提供内容。

当我进入浏览器并尝试访问http://localhost:8000时,回到主机框,它显示“Localhost 拒绝连接”。

在来宾盒子上运行netstat -tulnp | grep 8000,我得到以下信息:

tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      1909/node 

在主机箱上运行相同的程序,我得到以下输出:

tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      13827/VBoxHeadless

标签: javascriptreactjsvagrantvirtualboxgatsby

解决方案


我确实通过改变我在访客包厢上运行 Gatsby 的方式来完成这项工作。似乎gatsby develop -H 0.0.0.0会让 Gatsby 监听来自所有主机的连接。然后netstat -tulnp | grep 8000输出变为:

tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      2348/node

我正在回答我自己的问题,以防它对其他人有所帮助,但如果有人能想出一个更好的方法来做到这一点,而 Gatsby 仍然只在 localhost 上听,那么我更喜欢这个答案并接受这个答案。


推荐阅读