在Beego中切换从HTTP到HTTPS

huangapple go评论87阅读模式
英文:

Switch from HTTP to HTTPS in 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中工作吗?

英文:

I try to switch from HTTP to 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)
	}
}

And I am getting the following error:

crypto/tls: failed to parse key PEM data

My application is running in HTTP mode now and I want it to run in HTTPS mode.

Can anyone suggest how to make it work in HTTPS?

答案1

得分: 3

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

为了测试目的,可以使用crypto/tls包中的generate_cert.go文件生成有效的cert.pemkey.pem文件。

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

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";
英文:

The error indicates that the key.pem file cannot be parsed (could be invalid or lacking permission to read its content). Make sure the file is valid and sufficient permissions are set.

For testing purposes, use the generate_cert.go in the crypto/tls package to generate valid cert.pem and key.pem files.

To generate, run the following command (windows):

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"

huangapple
  • 本文由 发表于 2015年6月8日 17:21:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/30705387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定