nginx
nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server.
Able to support responses up to more than 50,000 concurrent connections, nginx chose epoll/kqueue as the network IO model.
nginx can also be used as a load balancing server. nginx is written in c language.
nginx is a main process and multiple worker processes, and the worker processes are single-threaded. Each worker process can handle client requests in an asynchronous, non-blocking manner.
【Extended learning asynchronous non-blocking and epoll select poll kqueue】
PHP in nginx is combined with nginx using the fastcgi method.
The client sends a request to the nginx server, and the nginx server hands the request to php for processing through fastcgi. The result of the php processing is returned to the nginx server through fastcgi.
Then the nginx server returns the result to the client.
Knowledge of fastcgi
CGI is a universal gateway interface. It is an interface standard between external applications and web servers, and a procedure for transferring information between external applications and web servers.
The cgi specification allows web servers to execute external programs and send their output to web browsers.
fastcgi is an improvement on cgi.
Fastcgi is like a memory-resident CGI. When a request arrives, CGI will first fork out a process to process the request.
However, when fastcgi starts loading, it starts multiple cgi interpreters and waits for the web server to connect.
When a client request reaches the webserver, the fastcgi process manager will select a cgi interpreter to handle the request.
The advantage is that the cgi interpreter is loaded into the memory and does not need to be read from the memory every time it is needed, which greatly improves the performance of the site. The problem with fastcgi is that when modifying the php configuration, it does not take effect immediately. Need to reload.
Blocking
A request comes from the client, and the web server receives the request. This request requires an IO operation.
The IO operation takes 10 seconds, then the web server waits for 10 seconds. During these 10 seconds, the web server rejects other requests. client request.
Non-blocking
A client request comes, and the web server receives the request. This request requires an io operation, and the io takes 10 seconds.
The web server can still receive other client requests during the 10 seconds of io operation. The web server puts the file descriptors of these requests into a queue, and when the io operation is ready, the data is sent to the client.
Asynchronous non-blocking io model
One master thread and multiple worker threads. One worker thread can handle multiple requests. If the requested event is not processed well, then the event file descriptor is placed in a queue
When the event is processed After that, read the results returned by the request, so a large number of concurrent requests can be processed. Of course, the concurrency here is only the unprocessed requests. Since the worker is a thread, only one request can be processed at the same time.
It’s just a constant switch between requests. The switch is also made proactively because the asynchronous event is not ready.
There is no cost to switching here. You can understand it as processing multiple prepared events in a loop, which is actually the case.
Compared with multi-threading, this event processing method has great advantages. There is no need to create threads, each request occupies very little memory, and there is no context switching.
Event processing is very lightweight. No matter how many concurrencies there are, it will not lead to unnecessary waste of resources (context switching)
The above introduces nginx learning and organization, including nginx content. I hope it will be helpful to friends who are interested in PHP tutorials.