如何在Beego中支持HTTPS

时间:2018-11-09 02:41:18

标签: go https beego

我希望beego网站支持https。

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


环境

  • go版本go1.10 Windows / amd64
  • Beego:1.10.1

我的步骤是:

  1. 在我的Windows 10计算机上安装googleapis.cer。
  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

  5. 运行我的项目

当我进入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.

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

1 个答案:

答案 0 :(得分:1)

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上设置的,这是数据竞争。

因此,除非您要修补beego本身,否则我建议您仅在HTTPS上运行您的应用。

例如在您的app.conf中:

EnableHTTP = false
EnableHTTPS = true