Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the pit of nginx proxy socket.io service

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to solve the pit of nginx proxy socket.io service". In the daily operation, I believe that many people have doubts about how to solve the problem of the pit of nginx proxy socket.io service. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to solve the pit of nginx proxy socket.io service". Next, please follow the editor to study!

Nginx proxies two socket.io servers. Socket.io 's working mode is to upgrade from polling to websocket

Phenomenon

When requesting services through nginx, there are a large number of 400 errors, sometimes you can upgrade to websocket, and sometimes you keep reporting errors. However, when accessed directly through the ip+ port, it will be 100% successful.

Analysis.

Sid

Sid is the key to our problem. When the connection is initially created (the polling mode simulates a persistent connection), the client initiates a request like this:

Https://***/?eio=3&transport=polling&t=1540820717277-0

After receiving it, the server creates an object, binds it to the connection, and returns a sid (session id) to mark the session. What does a conversation mean? a session is a series of interactions that are connected, and in our scenario, the next time the http request arrives, I need to find the object that was previously bound to the theoretical persistent connection (there is no websocket here, so it is theoretical). We know that http requests are stateless and each request is independent, so socket.io introduced sid to do this. After receiving the request, the server will generate a sid. Take a look at response:

The copy code is as follows:

{"sid": "eogal3frqlptoalp5est", "upgrades": ["websocket"], "pinginterval": 8000, "pingtimeout": 10000}

After that, you need to bring this sid with each request, and establishing a connection to the websocket request is no exception. So sid is the key to polling and the upgrade from polling to websocket. The request after that is similar to:

Https://***/?eio=3&transport=polling&t=1540820717314-1&sid=eogal3frqlptoalp5estorwss://***/?eio=3&transport=websocket&t=1540820717314-1&sid=eogal3frqlptoalp5est

So the question is, what if the request comes with a sid that is not generated by the server? The server will not recognize it. I will return a 400 to you and tell you

Invalid sid

This is the problem we encounter. The default load balancing strategy of nginx is polling, so the request may be called to the machine that did not generate the sid. At this time, we will receive a 400. if we are lucky, we may also call the original machine, with better luck, and even persist until the websocket connection is established.

Solve

Two schemes are proposed here.

Nginx's load balancing uses ip_hash, which ensures that all requests from a client go to one server.

Do not use polling mode, only use websocket

These two schemes have their own advantages and disadvantages. Second, it is obvious that old browsers and clients that do not support websocket will not work. The first kind of problem is hidden deeply. Imagine what happens if you add or subtract the machine. At this time, the mode of the ip_hash policy will change, and all the previous connections will fail. For micro services, expansion and reduction is very frequent (especially when the product is in the development stage). This kind of harmful expansion and reduction is very likely unacceptable.

At this point, the study on "how to solve the pit of nginx proxy socket.io service" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report