+ Nginx with PHP interaction in two ways communication with PHP Nginx

 

A, Nginx and PHP interaction of 7-step (dynamic PHP pages users access the process)

step1: user nginx http request to the server (user and the three-way handshake nginx server TCP connection)
Step2: nginx be determined according to the request URI and user access suffix
step3: As can be seen by the second step, the user's request dynamic content, nginx fastcgi request will be handed over to the client, will be sent via fastcgi_pass user's request to php-fpm
if a user visits it is a static resource, it is simple, nginx returns directly to the static resource requested by the user to the user .
step4: after receiving the request wrapper php-fpm turn over, wrapper will create a new thread calls the php dynamic program analysis server
step5: php will query the results back to the nginx
step6: nginx construct a response message and returns the result user
this is just one of the nginx user requests and return the results of a user request is asynchronous, that is, the user requests a transfer of resources to do in the nginx, nginx can be synchronized, that is parsed resources directly to the server resources returned to the user, do not do a transit in the nginx. Step 4: fastcgi_pass dynamic resource to php-fpm, php-fpm resources will be transferred wrapper php script resolution server

Namely: Nginx -> FastCGI -> php-fpm -> FastCGI Wrapper -> php resolver

CGI是通用网关协议,FastCGI则是一种常驻进程的CGI模式程序。我们所熟知的PHP-FPM的全称是PHP FastCGI Process Manager,即PHP-FPM会通过用户配置来管理一批FastCGI进程,例如在PHP-FPM管理下的某个FastCGI进程挂了,PHP-FPM会根据用户配置来看是否要重启补全,PHP-FPM更像是管理器,而真正衔接Nginx与PHP的则是FastCGI进程

图中,FastCGI的下游CGI-APP就是PHP程序。而FastCGI的上游是Nginx,他们之间有一个通信载体,即图中的socket。在我们上文图3的配置文件中,fastcgi_pass所配置的内容,便是告诉Nginx你接收到用户请求以后,你该往哪里转发,在我们图3中是转发到本机的一个socket文件,这里fastcgi_pass也常配置为一个http接口地址(这个可以在php-fpm.conf中配置)。而上图5中的Pre-fork,则对应着我们PHP-FPM的启动,也就是在我们启动PHP-FPM时便会根据用户配置启动诸多FastCGI触发器(FastCGI Wrapper)

PHP提供SAPI面向Webserver来提供扩展编程。但是这样的方式意味着你要是自主研发一套Webserver,你就需要学习SAPI,并且在你的Webserver程序中实现它。这意味着你的Webserver与PHP产生了耦合。解决耦合的办法:CGI协议,比较好的方式是有一套通用的规范,上下游都兼容它。那么CGI协议便成了Nginx、PHP都愿意接受的一种方式,而FastCGI常住进程的模式又让上下游程序有了高并发的可能。

二、Nginx与PHP的两种通信方式-unix socket和tcp socket

1、两者Nginx配置

unix socket

需要在nginx配置文件中填写php-fpm运行的pid文件地址。

  1.  
    location ~ \.php$ {
  2.  
    include fastcgi_params;
  3.  
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;
  4.  
    fastcgi_pass unix:/ var/run/php5-fpm.sock;
  5.  
    fastcgi_index index.php;
  6.  
    }

tcp socket

需要在nginx配置文件中填写php-fpm运行的ip地址和端口号。

  1.  
    location ~ \.php$ {
  2.  
    include fastcgi_params;
  3.  
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;
  4.  
    fastcgi_pass 127.0. 0. 1 : 9000;
  5.  
    fastcgi_index index.php;
  6.  
    }

2、两者比较

从上面的图片可以看,unix socket减少了不必要的tcp开销,而tcp需要经过loopback,还要申请临时端口和tcp相关资源。但是,unix socket高并发时候不稳定,连接数爆发时,会产生大量的长时缓存,在没有面向连接协议的支撑下,大数据包可能会直接出错不返回异常。tcp这样的面向连接的协议,多少可以保证通信的正确性和完整性。

3、选择建议:如果是在同一台服务器上运行的nginx和php-fpm,并发量不超过1000,选择unix socket,因为是本地,可以避免一些检查操作(路由等),因此更快,更轻。 如果面临高并发业务,我会选择使用更可靠的tcp socket,以负载均衡、内核优化等运维手段维持效率。

转载:

https://www.cnblogs.com/mangguoxiansheng/p/5967745.html

https://www.imooc.com/article/19278

https://blog.csdn.net/xing_____/article/details/78771113

https://blog.csdn.net/qq624202120/article/details/60957634

 

Guess you like

Origin www.cnblogs.com/applelife/p/11955531.html