以下の要件を満たして hugo server
を立ち上げたいという要求がありテンポラリで対応することになった。
hugo server
はローカルではなく、ある GCE インスタンスで実行する
- ローカルから繋ぎたいが、ポートフォワードは使わない
この要件を満たすためには、
- GCE インスタンスに :1313 でつなぎに行けるようにポートを開ける (ファイアウォールの設定)
- ポートフォワードは使えないので、グローバル IP を取る (とりあえず Ephemeral)
以下を参考に Firewall rule を設定して、GCE インスタンスにアプライした。
How to open a specific port such as 9090 in Google Compute Engine - Stack Overflow
動作確認として適当に Serve するスクリプトで :1313 を LISTEN して nmap してみた。
package main
import (
"net/http"
"io"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
}
func main() {
http.HandleFunc("/", helloHandler)
http.ListenAndServe(":1313", nil)
}
$ nmap -p 1313 <ip_address>
...
PORT STATE SERVICE
1313/tcp open bmc_patroldb
Nmap done: 1 IP address (1 host up) scanned in 0.37 seconds
問題なさそうなので、
[b4b4r07@instance-1 site] hugo server
次はこれで行けるかとローカルから http://<ip_address>:1313/
でリクエストすると Connection refused になった。
ps
しても hugo プロセスはいるし、
[b4b4r07@instance-1 site] curl localhost:1313
しても問題ない。
しかし、よく hugo の STDOUT を見ると、
...
Total in 11 ms
Watching for changes in /home/user/site/{content,data,layouts,static,themes}
Watching for config changes in /home/user/site/config.toml
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
bind address が 127.0.0.1 になっていた。
これだとローカルからのリクエストしか受け付けないので、次のようにして起動し直した。
[b4b4r07@instance-1 site] hugo server --bind 0.0.0.0
...
Web Server is available at http://localhost:1313/ (bind address 0.0.0.0)
Press Ctrl+C to stop
手元から http://<ip_address>:1313/
すると 200 になった。
これの理解にはこの記事が役立った。
127.0.0.1 を指定したらローカルホストからで、0.0.0.0 だと外部からも参照できるくらいにしか考えたことがなかったので、この機会を得たことでいい勉強になった。