Long Polling with Nginx - advice and clarification

扶醉桌前 提交于 2019-12-23 05:23:24

问题


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?


回答1:


client_body_timeout and client_header_timeout are probably what you're looking for

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

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.

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.



来源:https://stackoverflow.com/questions/17713305/long-polling-with-nginx-advice-and-clarification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!