无法使用NginX/FastCGI建立WebSocket连接。

huangapple go评论79阅读模式
英文:

Websocket connections cannot be made with NginX/FastCGI

问题

我有一个用Go编写的Web应用程序。当我使用标准库自带的Web服务器运行应用程序时,WebSocket连接正常工作。

然而,当我将Web应用程序作为FastCGI应用程序运行,并使用NginX作为代理时,无法建立WebSocket连接。我收到以下错误消息:

websocket: response does not implement http.Hijacker

我正在使用Gorilla Toolkit的WebSocket库进行WebSocket连接,并进行以下设置:

Go处理程序:

func NotificationsWebSocket(w http.ResponseWriter, r *http.Request) {
    ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
    if err != nil {
        log.Print(err.Error())
        return
    }
    //...
}

//...

router.HandleFunc("/notifications", NotificationsWebsocket)

//...

tcp, err := net.Listen("tcp", "127.0.0.1:9000")
handleErr(err)
err = fcgi.Serve(tcp, router)

nginx.conf

location ~ ^.+$ {
    fastcgi_pass 127.0.0.1:9000;
    include /etc/nginx/fastcgi_params;
}

我可以通过为所有常规Web处理程序设置一个FastCGI连接,并单独运行一个Web服务器来处理WebSocket处理程序,然后在NginX中相应地处理/notifications路径,但如果可能的话,我想避免这样做。

有人知道是否有一种方法可以配置NginX允许将连接升级为WebSocket吗?

英文:

I have a web application written in Go. When I run the application with the web server that comes with the standard library, websocket connections work fine.

However, when I run the web application as a FastCGI application and use NginX as a proxy, the websocket connection cannot be established. I get this error message:

websocket: response does not implement http.Hijacker

I am using Gorilla Toolkit's websocket library for websocket connections and have the following setup:

Go Handler:

func NotificationsWebSocket(w http.ResponseWriter, r *http.Request) {
    ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
    if err != nil {
        log.Print(err.Error())
        return
    }
    //...
}

//...

router.HandleFunc("/notifications", NotificationsWebsocket)

//...

tcp, err := net.Listen("tcp", "127.0.0.1:9000")
handleErr(err)
err = fcgi.Serve(tcp, router)

nginx.conf

location ~ ^.+$ {
    fastcgi_pass 127.0.0.1:9000;
    include /etc/nginx/fastcgi_params;
}

One way I could go about this is have a FastCGI connection for all the regular web handlers and run a separate web server just for the websocket handler, and then handle the /notifications path in NginX accordingly, but I want to avoid that if at all possible.

Does anyone know if there's a way to configure NginX to allow the connection to be upgraded to websockets?

答案1

得分: 3

你遇到的错误是 fcgi 响应不是 Hijacker(WebSockets 使用 hijacker 接口接管 TCP 套接字)。

这与 nginx 版本无关。

此外,FCGI 与 WebSockets 不兼容,你需要另一个服务器和反向代理。

英文:

the error you are getting is that the fcgi response is not also a Hijacker (websockets use the hijacker interface to take over the tcp socket).

It doesn't have anything to do with nginx versions.

Also, FCGI is not compatible with websockets, you need to have another server and reverse proxy to it

huangapple
  • 本文由 发表于 2014年3月4日 10:26:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/22161649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定