在Beego中从HTTP切换到HTTPS

时间:2015-06-08 09:21:52

标签: http go https beego

我尝试从HTTP切换到HTTPS:

func handler(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "text/plain")
    w.Write([]byte("This is an example server.\n"))
}

func main() {
    http.HandleFunc("/", handler)
    log.Printf("About to listen on 8080. Go to https://127.0.0.1:8080/")
    err := http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)
    if err != nil {
        log.Fatal(err)
    }
}

我收到以下错误:

crypto/tls: failed to parse key PEM data

我的应用程序现在以HTTP模式运行,我希望它以HTTPS模式运行。

有人可以建议如何让它在HTTPS中运行吗?

1 个答案:

答案 0 :(得分:3)

该错误表示无法解析key.pem文件(可能无效或缺少读取其内容的权限)。确保文件有效且设置了足够的权限。

出于测试目的,请使用crypto/tls包中的generate_cert.go生成有效的cert.pemkey.pem文件。

要生成,请运行以下命令(窗口):

go run %GOROOT%/src/crypto/tls/generate_cert.go -host="127.0.0.1"

Linux的:

go run $GOROOT/src/crypto/tls/generate_cert.go -host="127.0.0.1"
相关问题