仅仅修改这个还是不能保持keepalive,还需要在ngx_http_fastcgi_module.c中的ngx_http_fastcgi_finalize_request函数中增加这么一段,确保nginx保持连接,也就是设置
u->length=0,以确保keepalive模块能够判断是否要保持连接。
1917 ngx_http_fastcgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1918 {
1919 ngx_http_upstream_t *u = r->upstream;
1920 if(u != NULL)
1921 {
1922 u->length = 0;
1923 }
1924 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1925 "finalize http fastcgi request");
1926
1927 return;
1928 }
为了让nginx及时返回信息给客户端,
还需要修改src/event/ngx_event_pipe.c
原来代码如下:
while (cl && n > 0) {
ngx_event_pipe_remove_shadow_links(cl->buf);
size = cl->buf->end - cl->buf->last;
if (n >= size) {
cl->buf->last = cl->buf->end;
/* STUB * / cl->buf->num = p->num++;
if (p->input_filter(p, cl->buf) == NGX_ERROR){
return NGX_ABORT;
}
n -= size;
ln = cl;
cl = cl->next;
ngx_free_chain(p->pool, ln);
} else {
cl->buf->last += n;
n = 0;
}
}
修改如下:
while (cl && n > 0) {
ngx_event_pipe_remove_shadow_links(cl->buf);
size = cl->buf->end - cl->buf->last;
if (n >= size) {
cl->buf->last = cl->buf->end;
n -= size;
} else {
cl->buf->last += n;
n = 0;
}
/* STUB */cl->buf->num = p->num++;
if (p->input_filter(p, cl->buf) == NGX_ERROR) {
return NGX_ABORT;
}
ln = cl;
cl = cl->next;
ngx_free_chain(p->pool, ln);
}
经过上面修改,执行程序,抓包分析如下: