英文:
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.pem
和key.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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论