使用局域网中的移动设备连接到本地构建的 Jekyll 服务器

在一台计算机上使用 jekyll serve之后,就建立了一个 WEBrick 服务器,站点可以从这台特定 PC 上的 localhost:4000访问。

然而,我想知道如何从局域网内的其他机器访问这个网络服务器,尤其是移动设备?我正试图在移动设备上测试 Jekyll 站点,然后再把代码推到 Github 上。

24424 次浏览

Try jekyll serve --host=0.0.0.0 when you invoke Jekyll on the command line.

That will make Jekyll's HTTP server bind to all available IPs, rather than just to localhost.

You can also add this to your _config.yml with host: 0.0.0.0. GitHub will simply ignore this when you push, so it's safe to use if you don't mind having your work openly accessible on your network.


Without --host=0.0.0.0 Jekyll will output something like this when you start up:

$ jekyll serve
[...]
Server address: http://127.0.0.1:4000/
Server running... press ctrl-c to stop.

But with --host=0.0.0.0 (or host: 0.0.0.0 in _config.yml) you'll notice that it's listening on all interfaces (represented by 0.0.0.0) rather than just listening on the loopback interface (represented by 127.0.0.1)

$ jekyll serve --host=0.0.0.0
[...]
Server address: http://0.0.0.0:4000/
Server running... press ctrl-c to stop.

If you still cannot access your server then there might be a firewall stopping it. Temporarily disable your firewall, or add a port forwarding rule for port 4000.


Once Jekyll is appropriately listening on all interfaces, you can access this from your mobile device using your LAN IP address (retrieved from something like ifconfig or ipconfig depending on your operating system).

Assuming your mobile device is connected to the same LAN as your development machine.

  1. Assertain the LAN IP address of your development machine. Usually something like: 192.168.0.XXX. Where .XXX is the unique last 3 digits of your dev machine's LAN IP.

  2. Point your mobile device's web browser to: http://192.168.0.XXX:4000

That's how I do it on my laptop and iPhone for Jekyll dev.