浏览 5.2k
无意之间 突发奇想,设置如下nginx conf
upstream upserver {
server 127.0.0.1:12349;
### keepalive 100;
}
server {
listen 12347;
location /hello {
proxy_pass http://upserver;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
即upstream关闭 keepalive,但是server里明确指定 HTTP 1.1协议
通过tcpdump抓包可以看到nginx端使用HTTP 1.1协议请求upstream server,请求完毕之后发送FIN包关闭连接了!!!
这是什么原因呢? 百思不得其解,网络也没有这个问题的明确解答,源码里也找不到相应的代码逻辑之处

按点赞数排序
按时间排序
proxy_http_version为1.1,同时Connection: keep-alive,这只是说:HTTP协议是支持长连接的。但客户端要不要使用长连接呢?如果使用长连接,就得有连接池,那么连接池应该是多少呢?毕竟一旦有连接池,就可能存在资源浪费,在用户没有明确的配置keepalive连接池时,Nginx还是不会使用长连接。
1
回答于2020-09-10 11:41
翻了好久的nginx源码,大概清晰了。
ngx_http_upstream.c文件 ngx_http_upstream_finalize_request函数
if (u->peer.connection) { // 如果upstream peer connection不为null,则会关闭连接
ngx_close_connection(u->peer.connection);
}
ngx_http_upstream_keepalive_module.c文件 ngx_http_upstream_free_keepalive_peer函数
ngx_queue_insert_head(&kp->conf->cache, q); // 将free数组里的连接 放回到cached数组里
pc->connection = NULL; // 如果配置了keepalive属性,此时会将connections设置为null,避免被 ngx_http_upstream_finalize_request() 关闭连接
ngx_http_upstream_round_robin.c文件 ngx_http_upstream_free_round_robin_peer函数
没有对connection做任何设置,connection不为null
0
回答于2020-09-11 11:26
nginx-1.12.1,nginx-1.22.0 没复现这个问题。windows 是用的哪个版本? nginx 是哪个版本? 试下用administrator 权限。换台机器试试。
自问自答了。看了一下代码,感觉ngx_http_compile_complex_value和ngx_http_script_compile是两套实现,前者对后者进行了封装,使用起来应该更方便。
比如,如果有一个指令需要支持复杂变量,使用ngx_http_compile_complex_value只需要定义一个ngx_http_complex_value_t变量就可以。但是使用ngx_http_script_compile需要定义flushes,lengths,variables一系列数组。。
微信公众号
加入微信群