使用caddy代理的WebSocket握手错误

使用caddy代理的WebSocket握手错误,websocket,gorilla,caddy,Websocket,Gorilla,Caddy,我正在尝试在chrome浏览器客户端和服务器之间启动一个websocket连接 我的实施概述: 有一组不同的启动和运行项目。主项目是所有其他项目的中心,它处理到其他子项目的所有http请求、路由和代理。所有这些项目都使用负载平衡器。我的尝试是创建一个从chrom浏览器到一个子项目的websocket连接 助手版本:0.9.3 websocket库: 主项目的助手配置: https://{$DOMAIN_NAME}/analytics/ { tls ../resources/securit

我正在尝试在chrome浏览器客户端和服务器之间启动一个websocket连接

我的实施概述: 有一组不同的启动和运行项目。主项目是所有其他项目的中心,它处理到其他子项目的所有http请求、路由和代理。所有这些项目都使用负载平衡器。我的尝试是创建一个从chrom浏览器到一个子项目的websocket连接

助手版本:0.9.3
websocket库:

主项目的助手配置:

https://{$DOMAIN_NAME}/analytics/ {
    tls ../resources/security/server.pem ../resources/security/server.key
    proxy /  https://localhost:8107/analytics {
       websocket
       insecure_skip_verify
    }
}
localhost:{$ANALYTICS_CADDY_PORT}/analytics {
    root  webapps/analytics
    gzip
    ext    .html
    tls {$ANALYTICS_CERTIFICATE_FILE} {$ANALYTICS_KEY_FILE}
    proxy  /api https://localhost:{$ANALYTICS_HTTPS_PORT} {
         websocket
         insecure_skip_verify

    }
}
子项目的助手配置:

https://{$DOMAIN_NAME}/analytics/ {
    tls ../resources/security/server.pem ../resources/security/server.key
    proxy /  https://localhost:8107/analytics {
       websocket
       insecure_skip_verify
    }
}
localhost:{$ANALYTICS_CADDY_PORT}/analytics {
    root  webapps/analytics
    gzip
    ext    .html
    tls {$ANALYTICS_CERTIFICATE_FILE} {$ANALYTICS_KEY_FILE}
    proxy  /api https://localhost:{$ANALYTICS_HTTPS_PORT} {
         websocket
         insecure_skip_verify

    }
}
在分析子项目中,“/api/ws”将触发CreateSocketConnection()方法

CreateSocketConnection实现:

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)
}
conn = new WebSocket("wss://xxxx.com/analytics/api/ws");
客户端实现:

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)
}
conn = new WebSocket("wss://xxxx.com/analytics/api/ws");
问题是我在后端没有收到任何错误日志,但浏览器上的套接字连接失败

WebSocket connection to 'wss://xxxx.com/analytics/api/ws' failed: Error during WebSocket handshake: Unexpected response code: 502
请求标头:

Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:Upgrade
Cookie:username=admin; tenantid=1; tenantdomain=super.com; 
DNT:1
Host:xxxx.com
Origin:https://xxxx.com
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:O/DS1lRHzXptoWz5WR131A==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36
但响应标题如下所示:

Content-Encoding:gzip
Content-Length:40
Content-Type:text/plain; charset=utf-8
Date:Sat, 29 Oct 2016 03:13:23 GMT
Server:Caddy
Vary:Accept-Encoding
X-Content-Type-Options:nosniff
map[
    Connection:[Upgrade] 
    X-Forwarded-For:[127.0.0.1, 127.0.0.1] Dnt:[1] 
    Origin:[https://xxxx.com] 
    Pragma:[no-cache] 
    Sec-Websocket-Extensions:[permessage-deflate; client_max_window_bits] 
    Sec-Websocket-Version:[13] 
    Accept-Encoding:[gzip] 
    User-Agent:[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36] 
    Cache-Control:[no-cache] 
    Sec-Websocket-Key:[O/DS1lRHzXptoWz5WR131A==] 
    Upgrade:[websocket] 
    Cookie:[username=admin; tenantid=1; tenantdomain=super.com; ] 
    Accept-Language:[en-US,en;q=0.8]]
请注意,我在CreateSocketConnection方法中获得的请求头如下所示:

Content-Encoding:gzip
Content-Length:40
Content-Type:text/plain; charset=utf-8
Date:Sat, 29 Oct 2016 03:13:23 GMT
Server:Caddy
Vary:Accept-Encoding
X-Content-Type-Options:nosniff
map[
    Connection:[Upgrade] 
    X-Forwarded-For:[127.0.0.1, 127.0.0.1] Dnt:[1] 
    Origin:[https://xxxx.com] 
    Pragma:[no-cache] 
    Sec-Websocket-Extensions:[permessage-deflate; client_max_window_bits] 
    Sec-Websocket-Version:[13] 
    Accept-Encoding:[gzip] 
    User-Agent:[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36] 
    Cache-Control:[no-cache] 
    Sec-Websocket-Key:[O/DS1lRHzXptoWz5WR131A==] 
    Upgrade:[websocket] 
    Cookie:[username=admin; tenantid=1; tenantdomain=super.com; ] 
    Accept-Language:[en-US,en;q=0.8]]
我的实现中是否遗漏了什么


提前感谢

我遇到了类似的问题,我缺少的是
透明
标签

transparent
指定所有的头都应该随它一起发送,因此如果您有身份验证,这很重要

透明的:

将原始请求中的主机信息作为 大多数后端应用程序都希望如此。以下简称:

header_upstream Host {host}
header_upstream X-Real-IP {remote} header_upstream X-Forwarded-For {remote}
header_upstream X-Forwarded-Port {server_port}
header_upstream X-Forwarded-Proto {scheme}
资料来源: