英文:
Gorilla WebSocket disconnects after a minute
问题
我正在使用Go(Golang)1.4.2和Gorilla WebSockets,后面是一个nginx 1.4.6反向代理。我的WebSockets在打开页面后大约一分钟后会断开连接。在Chrome和Firefox上都有相同的行为。
起初,我在使用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));
Go报告了websocket: close 1006 unexpected EOF
错误。我知道当我离开或刷新页面时,ReadJSON
会返回EOF
,但这似乎是一个不同的错误。而且,这个意外的EOF会在打开页面大约一分钟后自动发生。
我在JavaScript中有一个onerror
函数,但该事件不会触发,而是触发了onclose
事件。
英文:
I'm using Go (Golang) 1.4.2 with Gorilla WebSockets behind an nginx 1.4.6 reverse proxy. My WebSockets are disconnecting after about a minute of having the page open. Same behavior occurs on Chrome and Firefox.
At first, I had problems connecting the server and client with WebSockets. Then, I read that I needed to tweak my nginx configuration. This is what I have.
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;
}
}
My Go code is basically echoing back the client's message. (Errors omitted for brevity). This is my 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)
}
The JavaScript is pretty simple as well.
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));
Go is reporting websocket: close 1006 unexpected EOF
. I know that when I leave or refresh the page ReadJSON
returns EOF
, but this appears to be a different error. Also, the unexpected EOF happens by itself after about a minute of having the page open.
I have an onerror
function in JavaScript. That event doesn't fire, but onclose
fires instead.
答案1
得分: 41
我遇到了同样的问题,问题出在nginx的配置上。它默认为proxy_pass
设置了1分钟的读取超时时间:
语法:proxy_read_timeout 时间;
默认值:proxy_read_timeout 60s;
上下文:http、server、location
请参考http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout
在我的情况下,我将超时时间增加到了10小时:
proxy_read_timeout 36000s;
英文:
I had the same issue, the problem is the nginx configuration. It defaults to a 1 minute read timeout for proxy_pass
:
> Syntax: proxy_read_timeout time;
>
> Default: proxy_read_timeout 60s;
>
> Context: http, server, location
See http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout
In my case I've increased the timeout to 10 hours:
proxy_read_timeout 36000s;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论