Nginx Agent WebSocket frequently interrupted solution, how to maintain long connection

Source: Internet
Author: User

background
这天气够热的,要处理的事情也够多的。。。。
    • 1
    • 2
want to see the solution, direct ctrl+f search keyword ' configuration point ' The beginning of the confession (to see the reason for this, personal opinion, does not mean right)

Explanation: Today with nginx anti-generation communication project, found that the average 1 minutes or so, there will be websocket connection interruption, and then look at a bit, is the problem of Nginx.

The reason is: nginx waiting for your first communication and the second communication time difference , more than it set the maximum waiting period , in short, the timeout , so it snapped a broken, Start the resolution step.

Solution One
 其实只要配置nginx.conf的对应localhost里面的这几个参数就好
    • 1
    • 2

1. Proxy_connect_timeout;
2. Proxy_read_timeout;
3. Proxy_send_timeout;

Solutions Two
发心跳包,原理就是在有效地再读时间内进行通讯,重新刷新再读时间
    • 1
    • 2

Note: The location of the configuration is localhost, giving a brief address

 http {server {location/{root html index index.html index.htm; Proxy_pass http://webscoket; proxy_http_version 1.1; proxy_ Connect_timeout 4s;  #配置点1 proxy_read_timeout 60s;  #配置点2, if not effective, you can consider this time configuration long proxy_send_timeout 12s;  #配置点3 proxy_set_header Upgrade $http _upgrade;  #这是webSocket的配置, unrelated to this blog Proxy_set_header Connection  #这是webSocket的配置, not related to this blog}}}         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
Explanation of Configuration 2

Note : This is the server for you to wait for the maximum time , that is, when you websocket using Nginx forwarding, with the above configuration 2, if there is no communication within 60 seconds , will still be disconnected , so you can set it according to your needs.

Example:
For example, I set 10 minutes, then if I have 10 minutes of communication, or 10 minutes of heartbeat, it is possible to keep the connection uninterrupted, detailed view of individual needs

Http://nginx.org/en/docs/http/websocket.html

Nginx Agent WebSocket frequently interrupted solution, how to maintain long connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.