duandu2159 2018-11-09 02:41
浏览 305
已采纳

如何在Beego中支持HTTPS

I want my beego website to support https.

There are another post Beego and Https. I try that method enable chrome setting chrome://flags/#allow-insecure-localhost or open url with Microsoft Edge. It's still show This site can’t be reached.


Environment

  • go version go1.10 windows/amd64
  • Beego : 1.10.1

My step is:

  1. Install googleapis.cer to my windows 10 computer.
  2. Copy googleapis.cer and googleapis.keyfile to D:\Go_workspace\src\myproject
  3. Edit 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. Run my project with the bee tool command ....\bin\bee run

I get the following message and show message This site can’t be reached when I go to 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.

Does anyone know how to solve this problem? Thank you

  • 写回答

1条回答 默认 最新

  • doxd96148 2018-11-09 06:52
    关注

    There is a a possible race condition in beego that makes it intermittent to run both HTTP and HTTPS together. You can see this in 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()
        }()
    }
    

    As you can see the Server.Addr is set on different goroutines which is a data race.

    So I would suggest that you run your app exclusively on HTTPS unless you want to patch beego itself.

    e.g. in your app.conf:

    EnableHTTP = false
    EnableHTTPS = true
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度