回答
收藏
分享
举报
nginx upstream keepalive 和 proxy_http_version之间的联系和区别
提问于2020-09-10 11:24

浏览 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包关闭连接了!!! 

这是什么原因呢? 百思不得其解,网络也没有这个问题的明确解答,源码里也找不到相应的代码逻辑之处


已修改于2023-03-16 13:39



写下您的回答
发表回答
全部回答(2)

按点赞数排序

按时间排序

proxy_http_version为1.1,同时Connection: keep-alive,这只是说:HTTP协议是支持长连接的。但客户端要不要使用长连接呢?如果使用长连接,就得有连接池,那么连接池应该是多少呢?毕竟一旦有连接池,就可能存在资源浪费,在用户没有明确的配置keepalive连接池时,Nginx还是不会使用长连接。

赞同

1

回复举报

回答于2020-09-10 11:41



回复陶辉
回复
nginx萌新回复了
这个行为逻辑不符合HTTP 1.1协议规范吧?

0

回复举报

回答于2020-09-10 03:47



回复nginx萌新
回复
陶辉回复了
@662:HTTP规范对语法语义是硬性规定的,但长连接不包括在内,双方都可以在自己认为资源不足时去关连接。

0

回复举报

回答于2020-09-10 05:38



回复陶辉
回复

翻了好久的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萌新
回复
提问者
nginx萌新
这家伙很懒还未留下介绍~
0
文章
1
问答
0
粉丝
相关问答

可以,我在《深入理解Nginx:模块开发与架构解析》第2版,第3.9节就介绍了C++ 如何编写Nginx模块的方法

点赞 0
浏览 4.1k

nginx-1.12.1,nginx-1.22.0 没复现这个问题。windows 是用的哪个版本? nginx 是哪个版本? 试下用administrator 权限。换台机器试试。

点赞 0
浏览 8.4k

自问自答了。看了一下代码,感觉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一系列数组。。


点赞 0
浏览 4k