我有一個(gè)非常簡(jiǎn)單的go https 網(wǎng)絡(luò)服務(wù)器這是代碼:package mainimport ( // "fmt" // "io" "net/http" "log")func HelloServer(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", "text/plain") w.Write([]byte("This is an example server.\n")) // fmt.Fprintf(w, "This is an example server.\n") // io.WriteString(w, "This is an example server.\n")}func main() { http.HandleFunc("/hello", HelloServer) err := http.ListenAndServeTLS(":8085", "fullchain.pem", "privkey.pem", nil)// err := http.ListenAndServeTLS(":8085", "certificate_ca.crt", "certificate.csr", nil) if err != nil { log.Fatal("ListenAndServe: ", err) }}它偵聽由防火墻打開的端口 8085。我使用 certbot 生成的 SSL 證書:sudo certbot certonly --standalone -d example.com所以我使用以下生成的文件構(gòu)建了 Web 服務(wù)器:err := http.ListenAndServeTLS(":8085", "fullchain.pem", "privkey.pem", nil)這是港口狀態(tài):$ sudo netstat -tulpn | grep 8085tcp6 0 0 :::8085 :::* LISTEN 23429/hello$ sudo ufw status | grep 80858085/tcp ALLOW Anywhere8085/tcp (v6) ALLOW Anywhere (v6)所以當(dāng)我嘗試從另一臺(tái)機(jī)器:$ curl -sL https://example.com:8085404 page not found在 DigitalOcean 上運(yùn)行的 Web 服務(wù)器。我需要以某種方式配置我的 Droplet 嗎?不確定我錯(cuò)過了什么?我還有certificate.crt, certificate_ca.crt, certificate.csr從賣給我域名的公司那里得到的文件。我應(yīng)該以某種方式使用這些文件嗎?我需要這個(gè)用于 OAuth 重定向 URI。
1 回答

明月笑刀無情
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
您已經(jīng)為 URL 路徑配置了處理程序/hello,但還沒有為路徑配置處理程序/。因此,當(dāng)您嘗試加載該路徑時(shí),您會(huì)得到 404。
如果您嘗試加載,https://example.com:8085/hello那么您會(huì)看到示例文本。
您還可以為 設(shè)置路線/,例如:
http.HandleFunc("/", HelloServer)
請(qǐng)注意,因?yàn)檫@會(huì)匹配所有可能的 URL,所以如果您只想匹配主頁,則需要明確檢查 URL,例如:
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
您還應(yīng)該考慮使用更靈活的多路復(fù)用器,例如 gorilla/mux。
- 1 回答
- 0 關(guān)注
- 112 瀏覽
添加回答
舉報(bào)
0/150
提交
取消