爲什麼發生這種情況,我能想到的原因有兩個
- 您的服務器應用程序不能訪問端口443
- 您的瀏覽器正試圖在端口到達你的服務器80
由於第一個問題無法通過標記標記解決,所以此答案將涵蓋第二種情況。
發生此問題,因爲在默認情況下,當你鍵入比如www.domain.com地址,瀏覽器嘗試使用端口80上的http協議聯繫地址域,它是一個衆所周知的行爲Golang ListenAndServeTLS returns data when not using https in the browser
現在,如果您在瀏覽器中輸入完整的URL(例如https://www.domain.com
),則瀏覽器將通過端口443接近服務器,並與服務器啓動TLS握手,從而呈現正確的數據。
現在,你知道這一點,但不是你的用戶。如果您的用戶在每次嘗試訪問您的Web應用程序時僅使用您的域名作爲URL,那麼您的用戶會收到SSL握手錯誤的通知,這實在令人沮喪。
爲了避免這個問題,你可以開始與端口的服務器一去例行:
// redir is a net.Http handler which redirects incoming requests to the
// proper scheme, in this case being https
func redir(w http.ResponseWriter, req *http.Request) {
hostParts := strings.Split(req.Host, ":")
http.Redirect(w, req, "https://"+hostParts[0]+req.RequestURI, http.StatusMovedPermanently)
}
func main() {
// this go subroutine creates a server on :8080 and uses the redir handler
go func() {
err := http.ListenAndServe(":8080", http.HandlerFunc(redir))
if err != nil {
panic("Error: " + err.Error())
}
}()
http.ListenAndServeTLS(":"+Config.String("port"), Config.Key("https").String("cert"), Config.Key("https").String("key"), router)
}
我希望:與此簡單的代碼重定向到443端口的所有請求80(或8080)它幫助 乾杯,
你的應用程序如何綁定到端口443?您是否設置了適當的功能,還是以root身份運行?您是否檢查返回的錯誤,以確定您的應用是否已成功綁定到端口,或者您是否正在與另一個進程通信? 'curl'的詳細輸出對連接說了什麼? – JimB
在黑暗中拍攝,但你嘗試去https:// ..? –