Nginx针对fastcgi保持keepalive的实验

为了保持与后端的连接,请下载keepalive模块( )

需要注意的是在多进程模式下,需要设置accept_mutex off;

假设你已经会用keepalive模块,我们继续分析在fastcgi如何保持连接?

nginx连接fastcgi默认情况下,是这样的(即使你设置了上面keepalive)

[root@localhost ~]# tcpdump -i lo -s 1500 port 9000
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 1500 bytes
15:23:16.901004 IP localhost.localdomain.50867 > localhost.localdomain.9000: S 3482201970:3482201970(0) win 32767 <mss 16396,sackOK,timestamp 2296841391 0,nop,wscale 7>
15:23:16.901025 IP localhost.localdomain.9000 > localhost.localdomain.50867: S 3473410857:3473410857(0) ack 3482201971 win 32767 <mss 16396,sackOK,timestamp 2296841391 2296841391,nop,wscale 7>
15:23:16.901039 IP localhost.localdomain.50867 > localhost.localdomain.9000: . ack 1 win 256 <nop,nop,timestamp 2296841391 2296841391>
15:23:16.901150 IP localhost.localdomain.50867 > localhost.localdomain.9000: P 1:1377(1376) ack 1 win 256 <nop,nop,timestamp 2296841391 2296841391>
15:23:16.901170 IP localhost.localdomain.9000 > localhost.localdomain.50867: . ack 1377 win 256 <nop,nop,timestamp 2296841391 2296841391>
15:23:16.901214 IP localhost.localdomain.9000 > localhost.localdomain.50867: P 1:97(96) ack 1377 win 256 <nop,nop,timestamp 2296841391 2296841391>
15:23:16.901222 IP localhost.localdomain.50867 > localhost.localdomain.9000: . ack 97 win 256 <nop,nop,timestamp 2296841391 2296841391>
15:23:16.901236 IP localhost.localdomain.9000 > localhost.localdomain.50867: F 97:97(0) ack 1377 win 256 <nop,nop,timestamp 2296841391 2296841391>
15:23:16.901822 IP localhost.localdomain.50867 > localhost.localdomain.9000: F 1377:1377(0) ack 98 win 256 <nop,nop,timestamp 2296841392 2296841391>
15:23:16.901836 IP localhost.localdomain.9000 > localhost.localdomain.50867: . ack 1378 win 256 <nop,nop,timestamp 2296841392 2296841392>

可以看出是后端主动关闭了连接,所以直接在nginx.conf配置文件中设置keepalive无效。

查看ngx_http_fastcgi_module.c文件,发现ngx_http_fastcgi_begin_request_t数据结构中有flags字段,

这个字段用来控制后端是否要主动关闭连接,

102 typedef struct {                                      
     103     u_char  role_hi;
     104     u_char  role_lo;                                  
     105     u_char  flags;     //通过这个来控制连接的关闭是否                               
     106     u_char  reserved[5];                              
     107 } ngx_http_fastcgi_begin_request_t;

fastcgi协议说明如下:

Closing Transport Connections
    The Web server controls the lifetime of transport connections. The Web server can close a connection when no requests are active. Or the Web server can delegate close authority to the application (see FCGI_BEGIN_REQUEST). In this case the application closes the connection at the end of a specified request.

再查看ngx_http_fastcgi_module.c文件中的设置:

475 static ngx_http_fastcgi_request_start_t  ngx_http_fastcgi_request_start = {
     476     { 1,                                               /* version */
     477       NGX_HTTP_FASTCGI_BEGIN_REQUEST,                  /* type */
     478       0,                                               /* request_id_hi */
     479       1,                                               /* request_id_lo */
     480       0,                                               /* content_length_hi */
     481       sizeof(ngx_http_fastcgi_begin_request_t),        /* content_length_lo */
     482       0,                                               /* padding_length */
     483       0 },                                             /* reserved */
     484
     485     { 0,                                               /* role_hi */
     486       NGX_HTTP_FASTCGI_RESPONDER,                      /* role_lo */
     487       0, /* NGX_HTTP_FASTCGI_KEEP_CONN */              /* flags */
     488       { 0, 0, 0, 0, 0 } },                             /* reserved[5] */
     489
     490     { 1,                                               /* version */
     491       NGX_HTTP_FASTCGI_PARAMS,                         /* type */
     492       0,                                               /* request_id_hi */
     493       1 },                                             /* request_id_lo */
     494
     495 };

我们把487行的0改为1,确保后端不主动关闭connection。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wywpsd.html