如何在Gatsby和Vagrant或VirtualBox上实现端口转发?

4

当我在Virtualbox上运行Vagrant时,使用Gatsby时无法进行端口转发。

Gatsby默认在本地主机端口8000上运行。

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

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

当我在Vagrant盒子上使用yarn develop启动Gatsby时,客户端看起来一切正常,应用程序正在localhost:8000上运行。
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
1个回答

9

我通过更改在虚拟机上运行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 的情况下完成此操作,我会优先考虑并接受该答案。为了帮助其他人,我将回答自己的问题。


1
我之前也遇到了同样的问题,这个答案完全解决了我的问题。谢谢! - daveskull81

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接