使用Nginx进行长轮询-建议和澄清 [英] Long Polling with Nginx - advice and clarification

查看:467
本文介绍了使用Nginx进行长轮询-建议和澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对长轮询请求进行澄清.我为长时间轮询制作了一个脚本,并在nginx(标准配置)上进行了尝试,并且可以正常工作.我注意到了一些事情. 60秒后,如果服务器不响应,nginx在日志浏览器中返回503错误,是否有可能(或应该)永不使请求过期,而仅在服务器响应时才终止?如果可能,我该怎么办?如果不可能,您是否建议我延长请求直到服务器停止(60秒)或更早(指示多少时间)?如果以前有,您如何建议我终止申请?

I'd like some clarification on the long-polling requests. I made a script for the long polling, I tried it on nginx (with standard configuration) and it works. I noticed some things. After 60 seconds if the server does not respond nginx returns the 503 error in the log browser, is possible (or should) not to never expire the request, but only when the server responds? If it is possible, how can I do? If not possible, do you advise me to stretch out the request until the server stops (60 seconds) or before (indicate how much time)? If before, how do you advise me to terminate the application?

推荐答案

client_body_timeout client_header_timeout

client_body_timeout 1m; # or 60s
client_header_timeout 1m;

将这些值更改为所需的时间,但是更常用的方法是例如60秒,然后服务器发送一个空响应(终止连接),然后客户端重新启动一个新请求,该请求还将持续60秒,等等.,原因是我认为,如果用户没有结束请求但实际上关闭了浏览器,例如,您将有很多无用的打开连接正在等待超时.

Change those values to how long you want, but the more commonly used method is for example 60 seconds then the server sends an empty response (terminate the connection), then the client restarts a new request, which will also last another 60 seconds, etc.., cause I believe otherwise if the user doesn't end the request but actually closes the browser for example, you would have a lot of useless open connections that are waiting to timeout.

编辑: 不要让nginx终止请求,让应用程序发送响应,否则您将收到504错误而不是200成功.如果您想进行60秒的长时间轮询,然后将nginx设置为90或120秒,那么当应用程序超过60秒时,它应该发送一个空输出,或者可能是一条消息,指出尚无类似'end的消息'或'stop',则javascript应在收到停止消息后启动新请求.还可以使用一些JavaScript,使服务器发送多个消息而无需使用分隔符或其他命令来终止连接,
我还要告诉您,一种方法适合所有工作,但并不总是很好,像chrome和firefox这样的浏览器可以使用更奇特的方法,例如服务器已发送事件 Web套接字,很多解析器库会检查当前浏览器支持哪些功能并使用最好的解析器,例如,如果您要使用服务器发送的事件(例如IE中尚不支持的事件),则可以使用类似的方法轻松处理此示例,然后退回长时间轮询仅在IE中.

EDIT: Don't make the request expire by nginx, let the application send the response, otherwise you'll get a 504 error not a 200 success. if you want to make your long poll for 60 seconds then set nginx for 90 or 120 seconds for example, then when the application exceeds the 60 seconds it should send an empty output, or maybe a message that says there's no message yet like 'end' or 'stop', the javascript then should initiate a new request after receiving the stop message. also with a bit of javascript you could make the server send multiple messages without terminating the connection by using a separator or something,
Also I would like to tell you that one method fits all works but isn't always good, browsers like chrome and firefox can use fancier methods like Server Sent Events or Web Sockets, there's a lot of parser libraries that check which functions are supported by the current browser and uses the best one, if you're going to use server sent events for example which aren't supported in IE yet, you could easily handle that with something like this example, and fall back to long polling only in IE.

这篇关于使用Nginx进行长轮询-建议和澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆