go - Beego如何支持HTTPS

标签 go https beego

我想让我的beego网站支持https。

还有一个帖子Beego and Https .我尝试使用该方法启用 chrome 设置 chrome://flags/#allow-insecure-localhost 或使用 Microsoft Edge 打开 url。仍然显示无法访问此站点


环境

  • go版本go1.10 windows/amd64
  • 蜜蜂:1.10.1

我的步骤是:

  1. 将 googleapis.cer 安装到我的 Windows 10 计算机上。
  2. 将 googleapis.cer 和 googleapis.keyfile 复制到 D:\Go_workspace\src\myproject
  3. 编辑 D:\Go_workspace\src\myproject\conf\app.conf

    appname = myproject
    runmode = prod
    [dev]
    httpaddr = "127.0.0.1"
    HTTPPort = 9100
    [prod]
    httpaddr = "127.0.0.1"
    HTTPSPort = 9099
    httpsaddr = "127.0.0.1"
    EnableHTTPS = true
    EnableHttpTLS = true
    HTTPSCertFile = "googleapis.cer"
    HTTPSKeyFile = "googleapis.key"  
    [test]
    HTTPSPort = 9099
    
  4. 使用蜜蜂工具命令运行我的项目....\bin\bee run

当我转到 URL https://127.0.0.1:9099 时,我收到以下消息并显示消息无法访问此站点 :

2018/11/09 10:07:56.251 [I] [asm_amd64.s:2361]  http server Running on http://127.0.0.1:8080
2018/11/09 10:07:56.253 [I] [asm_amd64.s:2361]  https server Running on https://127.0.0.1:9099
2018/11/09 10:07:56.293 [C] [asm_amd64.s:2361]  ListenAndServeTLS:  listen tcp 127.0.0.1:9099: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.

有谁知道如何解决这个问题?谢谢

最佳答案

beego 中可能存在竞争条件,这使得同时运行 HTTP 和 HTTPS 时断断续续。你可以在 app.go

中看到这个
if BConfig.Listen.EnableHTTPS || BConfig.Listen.EnableMutualHTTPS {
    go func() {
        //...
        app.Server.Addr = // the Addr is set to the value of HTTPS addr
        // ListenAndServeTLS()
    }()
}
if BConfig.Listen.EnableHTTP {
    go func() {
        app.Server.Addr = addr // the Addr is set to the valu of HTTP addr
        // ListenAndServe()
    }()
}

如您所见,Server.Addr 设置在不同的 goroutine 上,这是一场数据竞争。

所以我建议您只在 HTTPS 上运行您的应用程序,除非您想修补 beego 本身。

例如在你的 app.conf 中:

EnableHTTP = false
EnableHTTPS = true

关于go - Beego如何支持HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53219095/

相关文章:

go - 在端口 80 和 apache 中运行 beego

http - Golang CSRF 在结构中保存模板字段

go - "A"和 "A"的区别[0]

Powershell:Azure Web 应用程序 - 将 httpsOnly 设置为

html - https 工作正常但 *.com :443 gives a 400 error?

database - 如何使用beego orm限制范围

go - 如何在Go中打开进程并正确记录stdin和stdout?

go - golangsync.Map Range函数线程安全吗?

iphone - HTTPS + ASIHTTPRequest 会被 App Store 批准吗?

api - beego评论路由器