nginx和php-fpm保持长连接

个人学习笔记,谢绝转载!!!

原文:https://www.cnblogs.com/wshenjin/p/15724987.html


upstream phpfpm {
    server 127.0.0.1:9001;
    keepalive 10;
}

server{
    keepalive_requests 4096;
    location ~ .*\.php$
    {
         include fcgi.conf;
         fastcgi_pass phpfpm;
         fastcgi_index index.php;
         expires off;
         fastcgi_keep_conn on;
    }
}

1、upstream中的keepalive设置:
此处keepalive的含义不是开启、关闭长连接的开关;也不是用来设置超时的timeout;更不是设置长连接池最大连接数。官方解释:

  • The connections parameter sets the maximum number of idle keepalive connections to upstream servers connections(设置到upstream服务器的空闲keepalive连接的最大数量)

  • When this number is exceeded, the least recently used connections are closed. (当这个数量被突破时,最近使用最少的连接将被关闭)

  • It should be particularly noted that the keepalive directive does not limit the total number of connections to upstream servers that an nginx worker process can open.(特别提醒:keepalive指令不会限制一个nginx worker进程到upstream服务器连接的总数量)

2、keepalive_requests 指令用于设置一个keep-alive连接上可以服务的请求的最大数量。当最大请求数量达到时,连接被关闭。默认是100。
这个参数的真实含义,是指一个keep alive建立之后,nginx就会为这个连接设置一个计数器,记录这个keepalive的长连接上已经接收并处理的客户端请求的数量。如果达到这个参数设置的最大值时,则nginx会强行关闭这个长连接,逼迫客户端不得不重新建立新的长连接。
这里设置和php-fpm的pm.max_request一样

posted @ 2021-12-23 19:52  wshenJin  阅读(823)  评论(0编辑  收藏  举报