Nginx 竟然也有 playground:還是 Go 語言構建的
大家好,我是 polarisxu。
曾幾何時,playground 似乎成了新語言的標配:Go 發佈就帶有 https://play.golang.org/,Rust 發佈也有 https://play.rust-lang.org/。你想過 Nginx 也有一個 playground 嗎?你可以通過它方便的測試 Nginx 配置。
今天發現,還真有一個,地址:https://nginx-playground.wizardzines.com。關鍵是,後端使用 Go 構建的。
以下是該網址的截圖:
Nginx Playground
本文簡單介紹下這個 playground。
01 如何使用
打開這個網站後,在左側,你可以寫 Nginx 配置,在右上角可以通過 curl 命令或 http 命令(這是 httpie)來向該 Nginx 實例發送 HTTP 請求。
然後點擊右上角的 “Run”,成功後,在右下角會輸出:
1)如果 Nginx 啓動成功,輸出執行命令的結果;
2)如果 Nginx 無法啓動(配置出錯了),則會輸出 Nginx 錯誤日誌。
02 原理
這個網站使用的技術如下:
1)前端使用 vue.js 和 tailwind;
2)後端就一個 API endpoint,使用 Go 語言構建。它只做 1 件事,即運行 Nginx 配置。
後端的完整代碼見這裏:https://gist.github.com/jvns/edf78e7775fea8888685a9a2956bc477。
當你單擊 “Run” 時,Go 後端會執行以下操作:
-
將配置寫入臨時文件
-
創建一個新的網絡命名空間 (
ip netns add $RANDOM_NAMESPACE_NAME
) -
在端口 777 上啓動 go-httpbin,以便可以在 nginx 配置中使用它作爲 backend(如上面截圖中的 proxy_pass 地址)
-
啓動 Nginx
-
等待 100 毫秒以確保 nginx 啓動完成,如果失敗則將 nginx 的錯誤日誌返回給客戶端
-
運行用戶請求的命令(並確保命令以
curl
或開頭http
) -
返回命令的輸出
-
完畢
Go 後端一共 100 多行代碼,邏輯處理代碼 70 行左右,對實現感興趣的可以讀一下。
03 小結
這個網站的作者寫了一篇文章介紹它,包括安全問題、性能問題等,有興趣的可以查看:https://jvns.ca/blog/2021/09/24/new-tool--an-nginx-playground/。另外,還有一個 Nginx location match 測試的網址:https://nginx.viraptor.info/。
爲了方便無法訪問上面 Go 代碼的同學,我將完整 Go 代碼貼在下面:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"os/exec"
"strings"
"syscall"
"time"
)
type RunRequest struct {
NginxConfig string `json:"nginx_config"`
Command string `json:"command"`
}
type RunResponse struct {
Result string `json:"result"`
}
func main() {
rand.Seed(time.Now().UnixNano())
http.Handle("/", wrapLogger(Handler{runHandler}))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func runHandler(w http.ResponseWriter, r *http.Request) error {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
if r.Method != "POST" {
// OPTIONS request
return nil
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("failed to read body: %s", err)
}
var req RunRequest
json.Unmarshal([]byte(body), &req)
// write config
file, err := os.CreateTemp("/tmp", "nginx_config")
errorFile, err := os.CreateTemp("/tmp", "nginx_errors")
if err != nil {
return fmt.Errorf("failed to create temp file, %s", err)
}
file.WriteString(req.NginxConfig)
file.Close()
defer os.Remove(file.Name())
defer os.Remove(errorFile.Name())
// set up network namespace
namespace := "ns_" + randSeq(16)
if err := exec.Command("ip", "netns", "add", namespace).Run(); err != nil {
return fmt.Errorf("failed to create network namespace: %s", err)
}
defer exec.Command("ip", "netns", "delete", namespace).Run()
if err := exec.Command("ip", "netns", "exec", namespace, "ip", "link", "set", "dev", "lo", "up").Run(); err != nil {
return fmt.Errorf("failed to create network namespace: %s", err)
}
// start httpbin
httpbin_cmd := exec.Command("ip", "netns", "exec", namespace, "go-httpbin", "-port", "7777")
if err := httpbin_cmd.Start(); err != nil {
return fmt.Errorf("failed to start go-httpbin: %s", err)
}
defer kill(httpbin_cmd)
// start nginx
nginx_cmd := exec.Command("ip", "netns", "exec", namespace, "nginx", "-c", file.Name(), "-e", errorFile.Name(), "-g", "daemon off;")
if err != nil {
return fmt.Errorf("failed to get pipe: %s", err)
}
ch := make(chan error)
go func() {
ch <- nginx_cmd.Run()
}()
// Check for errors
select {
case <-ch:
logs, _ := os.ReadFile(errorFile.Name())
return fmt.Errorf("nginx failed to start. Error logs:\n\n %s", string(logs))
case <-time.After(100 * time.Millisecond):
defer term(nginx_cmd)
break
}
// run curl
curlArgs := strings.Split(strings.TrimSpace(req.Command), " ")
if curlArgs[0] != "curl" && curlArgs[0] != "http" {
return fmt.Errorf("command must start with 'curl' or 'http'")
}
curlCommand := append([]string{"netns", "exec", namespace}, curlArgs...)
output, _ := exec.Command("ip", curlCommand...).CombinedOutput()
// return response
resp := RunResponse{
Result: string(output),
}
response, err := json.Marshal(&resp)
if err != nil {
return fmt.Errorf("failed to marshal json, %s", err)
}
w.Header().Add("Content-Type", "application/json")
w.Write(response)
return nil
}
func wrapLogger(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rw := &responseWrapper{w, 200}
start := time.Now()
handler.ServeHTTP(rw, r)
elapsed := time.Since(start)
log.Printf("%s %d %s %s %s", r.RemoteAddr, rw.status, r.Method, r.URL.Path, elapsed)
})
}
func term(cmd *exec.Cmd) {
if cmd.Process != nil {
cmd.Process.Signal(syscall.SIGTERM)
}
}
func kill(cmd *exec.Cmd) {
if cmd.Process != nil {
cmd.Process.Kill()
}
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
我是 polarisxu,北大碩士畢業,曾在 360 等知名互聯網公司工作,10 多年技術研發與架構經驗!2012 年接觸 Go 語言並創建了 Go 語言中文網!著有《Go 語言編程之旅》、開源圖書《Go 語言標準庫》等。
堅持輸出技術(包括 Go、Rust 等技術)、職場心得和創業感悟!歡迎關注「polarisxu」一起成長!也歡迎加我微信好友交流:gopherstudio
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/vU5vhLY81vC2EJCLu4VuNA