猿问

Docker 上的 Websocket 与 nginx

我正在尝试构建一个包含两部分的服务:后端和前端。两者都在不同的 docker 容器上,并通过 docker-compose 配置和 nginx 容器进行通信。

对于https访问,一切都很好,但是当我尝试使用websocket时,我有一个升级错误,即使Nginx配置得到了这个信息

错误信息 :websocket: the client is not using the websocket protocol:  'upgrade' token not found in 'Connection' header

我正在使用fasthttpandfasthttp/websocket作为我的 Golang 后端。代码在 localhost 上运行(没有 nginx 配置),但是 Docker + nginx 的组合似乎破坏了一些东西。前端使用react并且是一个简单的let socket = new WebSocket(wss.mydomain.com/ws/uploadPicture/);

编辑 :

  • ctx.Request.Header.ConnectionUpgrade()之前使用时upgrader.Upgrade,结果是true,但是ctx.Response.Header.ConnectionUpgrade()是假的

谢谢 !


Golang 后端

var upgrader = websocket.FastHTTPUpgrader{

    ReadBufferSize:  1024,

    WriteBufferSize: 1024,

    CheckOrigin: func(ctx *fasthttp.RequestCtx) bool {

        return true

    },

}

func    InitRouter() func(*fasthttp.RequestCtx) {

    router := fasthttprouter.New()


    router.GET("/ws/uploadPicture/", doWS)


    return router.Handler

}

func    doWS(ctx *fasthttp.RequestCtx) {

    err := upgrader.Upgrade(ctx, func(conn *websocket.Conn) {

        //SHOULD DO STUFF

    })

    if (err != nil) {

        logs.Error(err.Error()) //HIT THIS ERROR

        return

    }

}


...

fasthttp.ListenAndServe(`:8000`, InitRouter())


紫衣仙女
浏览 404回答 3
3回答

大话西游666

我是 fasthttp 的 websocket 包的维护者。该问题已在 master 分支上修复。下载它:go get github.com/fasthttp/websocket@master请再试一次。如果问题仍然存在,请在https://github.com/fasthttp/websocket/issues中打开一个问题很快,我将发布一个新版本。

一只斗牛犬

事实证明这不是 Nginx/Docker 的问题,而是应该升级连接的 golang 中的 http 包。我将fasthttp与fasthttp/websocket一起使用,它应该可以工作,但它不在 docker 容器中。我尝试使用官方(而不是 fasthttp 的 fork)gorilla/websocket切换到httprouter ,并且连接成功升级。去查一下问题出在哪里!

Qyouu

我相信upgrade应该是一个字符串:proxy_set_header Connection "upgrade";
随时随地看视频慕课网APP

相关分类

Go
我要回答