duanfuxing2417 2018-06-21 11:16
浏览 361

Golang中Caddy代理的WebSocket握手错误

I’m trying to initiate a websocket connection between chrome browser client and server.

Overview of my implementation : There are set of different up and running projects. The load balancer to all other projects and it handle all http requests, routes and proxy to other sub projects. These all projects use load balancers. My attempt is to create a websocket connection from chrom browser to one sub project.

caddy version : 0.9.3

websocket go library : github.com/gorilla/websocket 1

// Starting caddy server to server static files
args := []string{"bin/caddy", "--conf=" + commons.ServerConfigurations.CaddyFile, "-pidfile=bin/caddy.pid"}

if err := exec.Command("nohup", args...).Start(); err != nil {
    log.Fatalln("Error occourred while starting caddy server : ", err.Error())
    os.Exit(1)
}

//Starting the API server
router := routes.NewRouter()
http.Handle("/", router)

httpsServer := &http.Server{
    Addr: ":" + strconv.Itoa(commons.ServerConfigurations.HttpsPort+
        commons.ServerConfigurations.PortOffset),
    Handler:        logHandler,
    ReadTimeout:    time.Duration(commons.ServerConfigurations.ReadTimeOut) * time.Second,
    WriteTimeout:   time.Duration(commons.ServerConfigurations.WriteTimeOut) * time.Second,
    MaxHeaderBytes: 1 << 20,
}

httpsServer.ListenAndServeTLS(commons.ServerConfigurations.SSLCertificateFile,
        commons.ServerConfigurations.SSLKeyFile)

First when client reach to the domain, $DOMAIN_NAME such as www.sample.com. Caddy proxy the client request to the https://localhost:8107/analytics

https://{$DOMAIN_NAME}/analytics/ {
    tls pem_file key_file
    proxy /  https://localhost:8105/analytics { 
       websocket
       insecure_skip_verify
    }
}

There are multiple standalone servers. Following is the caddy configuration of server.It serve static files at the directory in webapps/analytics

localhost:8105/analytics {
    root  webapps/analytics
    gzip
    ext    .html
    tls pem_file key_file
    proxy  /api https://localhost:8108 {  HTTPS port:8108
         websocket
         insecure_skip_verify
    }
}

Inside the analytics sub project, " /api/ws " would trigger CreateSocketConnection() method.

//Starting the API server
    router := routes.NewRouter()
    http.Handle("/", router)
    http.HandleFunc("/api/ws", api.CreateSocketConnection)

CreateSocketConnection implementation :

func CreateSocketConnection(w http.ResponseWriter, r *http.Request) {
    var upgrader = websocket.Upgrader{
        ReadBufferSize:  1024,
        WriteBufferSize: 1024,
    }
    _, err = upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Fatal("upgrader failed :", err.Error())
    }
    //controllers.HandleSocket(ws)
}

Client side implementation :

conn = new WebSocket("wss://xxxx.com/analytics/api/ws");

Websocket upgrade error happen in golang backend server

2018/06/21 16:53:41 http: TLS handshake error from 127.0.0.1:47216: remote error: tls: bad certificate

How I use this secure https and websocket in same web server.Is it posible ?

web request : 'wss://xxxx.com/analytics/api/ws' failed: Error during WebSocket handshake: Unexpected response code: 502

Am I missing something in my implementation?

Thanks in advance

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器