nginx acts as a proxy for two socket.io servers. The working mode of socket.io is polling and upgrading to websocket
Phenomena
When requesting services through nginx, a large number of 400 errors appear. Sometimes it can be upgraded to websocket. Sometimes it keeps reporting errors. But when accessing directly through ip port
, it will be 100% successful.
Analysis
sid
sid is the key to our problem. When initially creating a connection (polling mode is simulating a long connection), the client will initiate such a request:
https://***/?eio=3&transport=polling&t=1540820717277-0
After receiving it, the server will create an object, bind it to the connection, and return a sid (session id) to mark the session. What does a session refer to? A session is a series of interactions, and these interactions are related. In our scenario, when the next http request comes, I need to find the long connection that was previously bound to the theory (not yet here) websocket, so theoretically). We know that http requests are stateless and each request is independent, so socket.io introduced sid to do this. The server will generate a sid after receiving the request. Look at the response:
Copy code The code is as follows:
{"sid":"eogal3frqlptoalp5est","upgrades":["websocket"] ,"pinginterval":8000,"pingtimeout":10000}
Every subsequent request needs to bring this sid, and the connection to establish a websocket request is no exception. Therefore, sid is the key to polling and upgrading polling to websocket. The request after this is similar to:
https://***/?eio=3&transport=polling&t=1540820717314-1&sid=eogal3frqlptoalp5est or wss://***/?eio=3&transport=websocket&t=1540820717314-1&sid=eogal3frqlptoalp5est
Then the question is, what happens if the sid in the request is not generated by the server? The server will not recognize it, return a 400 to you, and tell you
invalid sid
This is the problem we encountered. The default load balancing strategy of nginx is polling, so the request may not be generated. If we go to the machine with this SID, we will receive a 400 at this time. If we are lucky, it may be sent to the original machine. With better luck, we can even persist until the websocket connection is established.
Solution
Two solutions are proposed here
nginx's load balancing uses ip_hash, which can guarantee a client's request All go to one server
Do not use polling mode, only use websocket
Both options have their own pros and cons. The second obvious one is that older browsers and clients that don't support websockets won't work. The first type of problem is hidden deeper. Just imagine what will happen if you add or remove machines. At this time, the mode of the ip_hash policy will change, and all previous connections will become invalid. For microservices, expansion and contraction are very frequent operations. (Especially when the product is in the development stage), this kind of lossy expansion and contraction is most likely unacceptable.
The above is the detailed content of How to solve the pitfall of nginx proxy socket.io service. For more information, please follow other related articles on the PHP Chinese website!

NGINX and Apache each have their own advantages, and the choice depends on the specific needs. 1.NGINX is suitable for high concurrency, with simple deployment, and configuration examples include virtual hosts and reverse proxy. 2. Apache is suitable for complex configurations and is equally simple to deploy. Configuration examples include virtual hosts and URL rewrites.

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

NGINXUnit is an open source application server that supports multiple programming languages and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
