大猩猩WebSocket在一分钟后断开连接
问题描述:
我使用Go(Golang)1.4.2和Gorilla WebSockets在nginx 1.4.6反向代理后面。打开页面大约一分钟后,我的WebSockets断开连接。 Chrome和Firefox出现同样的行为。大猩猩WebSocket在一分钟后断开连接
起初,我在使用WebSockets连接服务器和客户端时遇到问题。然后,我读到我需要调整我的nginx配置。这是我的。
server {
listen 80;
server_name example.com;
proxy_pass_header Server;
location/{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:1234;
}
}
我的Go代码基本上是回显客户端的消息。 (为简洁起见省略了错误)。这是我的HandleFunc
。
var up = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
ws, _ := up.Upgrade(resp, req, nil)
defer ws.Close()
var s struct {
Foo string
Bar string
}
for {
ws.ReadJSON(&s)
ws.WriteJSON(s)
}
JavaScript也很简单。
var ws = new WebSocket("ws://example.com/ws/");
ws.addEventListener("message", function(evnt) {
console.log(JSON.parse(evnt.data));
});
var s = {
Foo: "hello",
Bar: "world"
};
ws.send(JSON.stringify(s));
走的是报告websocket: close 1006 unexpected EOF
。我知道当我离开或刷新页面时ReadJSON
返回EOF
,但这似乎是一个不同的错误。此外,在页面打开约一分钟后,意外的EOF本身也会发生。
我在JavaScript中有一个onerror
函数。该事件不会触发,但是onclose
会发生。
答
我有同样的问题,问题是nginx配置。默认为1分钟读取超时proxy_pass
:
语法:proxy_read_timeout time;
默认值:proxy_read_timeout 60s;
上下文:http,服务器,位置
见http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout
在我来说,我已经加大了超时10小时:
proxy_read_timeout 36000s;
到目前为止好,你永远不知道,什么时候会跟你分享一些智慧。多谢,伙计! – 2015-05-09 12:21:42
@MaxYari伙计庇护 – 2015-05-09 15:20:57