Nginx based on epoll
With epoll, one process can theoretically have an unlimited number of connections without polling, which is truly Solved the problem of c10k. (Recommended learning: swoole video tutorial)
Nginx is an asynchronous non-blocking server program based on epoll. Naturally, it is understandable that Nginx can easily handle millions of concurrent connections.
How swoole handles high concurrency
①Reactor model introduction
IO multiplexing asynchronous non-blocking programs use the classic Reactor model. Reactor, as the name suggests, is a reactor. Meaning, it does not handle any data sending and receiving itself. It can only monitor the event changes of a socket (can also be a pipe, eventfd, signal) handle.
Note: What is a handle? The English word handle is handler, which can be vividly compared to the handle of a pot or a spoon. That is, the unique identifier of the resource and the ID of the resource. Resources can be operated through this ID.
Reactor is just an event generator. The actual operations on the socket handle, such as connect/accept, send/recv, and close, are completed in the callback.
Because reactor is based on epoll, each reactor can handle countless connection requests. In this way, swoole can easily handle high concurrency.
nginx configuration:
server { listen 80; server_name www.swoole.com; root /data/wwwroot/www.swoole.com; location / { if (!-e $request_filename){ proxy_pass http://127.0.0.1:9501; } } }
9501 is the address that the swoole server listens to. root is set to the directory of static files. When requesting a static file, it is processed directly by Nginx. When the requested file does not exist, it is sent to the Swoole server for processing.
The above is the detailed content of How to mix nginx and swoole. For more information, please follow other related articles on the PHP Chinese website!