回答
收藏
分享
举报
NGINX正向代理出现大量TIME_WAIT问题
提问于2022-07-18 14:36

浏览 4.2k

正向代理配置:

  http {
    server {
        listen 8080;
       # dns resolver used by forward proxying
       resolver 8.8.8.8;
       location / {
           proxy_http_version 1.1;
           proxy_set_header Connection "";
           proxy_set_header Host $host;
           proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
           proxy_pass $scheme://$host;
       }
    }
}  

在处理高并发请求代理时,NGINX后端会出现大量TIME_WAIT连接。

分析下来,NGINX每次代理转发请求时,都会根据应用发过来的请求来与后端服务建链,尽管设置http长连接属性,但每处理完一笔交易就会断开后端连接,长此以往就会出现大量TIME_WAIT连接。

如果并发压力比较大时,就瞬间出现大量TIME_WAIT连接,进而导致NGINX侧系统资源不足(随机端口不足)。


在这种模式下,如何解决TIME_WAIT问题?是否有类似方向代理upstream中配置连接池的方式来避免频繁断链?


已修改于2023-03-17 09:03



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

按点赞数排序

按时间排序

可以通过 proxy_set_header 设置,keepalive 。

赞同

0

回复举报

回答于2022-10-31 14:11



回复jiayouachunyu
回复
提问者
李永峰
打工人
0
文章
1
问答
0
粉丝
相关问答

ticket已经明确地表明了NGINX开发团队的态度,不支持环境变量实现正向代理,可以尝试使用stream配置proxy_pass转发数据,不过解决方案就不是那么简单了。

点赞 0
浏览 3.4k

我重新下载openresty1.19.3.2

同样的操作也成功了,


点赞 0
浏览 7.4k

nginx可以通过ngx_lua_waf实现waf。首先需要安装luajit运行时和nginx
一、luajit运行时安装参考openresty官方:
https://github.com/openresty/lua-nginx-module#installation
我使用的如下三个版本:luajit2-2.1-20220411.tar.gz lua-nginx-module-0.10.21.tar.gz ngx_devel_kit-0.3.1.tar.gz
二、nginx安装:
我的编译参数,--prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/home/lq/work/tongmingzhiyun/openresty/lua-nginx-module-0.10.21 --add-module=/home/lq/work/tongmingzhiyun/openresty/ngx_devel_kit-0.3.1
三、搭建waf防火墙:
参考ngx_lua_waf github仓库 readme,https://github.com/loveshell/ngx_lua_waf,需要注意的地方,
1.我的环境中其他lua包在/etc/nginx/lua/下,所以lua_package_path需要多加一个目录
lua_package_path '/opt/nginx/conf/waf/?.lua;/etc/nginx/lua/?.lua;;';
lua_shared_dict limit 10m;
init_by_lua_file /opt/nginx/conf/waf/init.lua;
access_by_lua_file /opt/nginx/conf/waf/waf.lua;
2.测试的时候不能使用127.0.0.1
curl http://127.0.0.1:80/test.php?id=../etc/passwd,防火墙args规则中白名单中有127.0.0.1,这样导致127.0.0.1的请求不会进行waf处理(这里需要注意,否则会花费大量时间排查),可以使用其他网卡地址,比如我使用ens33,
root@ubuntu:/opt/nginx# curl http://192.168.175.128:80/test.php?id=../etc/passwd
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>网站防火墙</title>
<style>
p {
line-height:20px;
}
ul{ list-style-type:none;}
li{ list-style-type:none;}
</style>
</head>

<body style=" padding:0; margin:0; font:14px/1.5 Microsoft Yahei, 宋体,sans-serif; color:#555;">

<div style="margin: 0 auto; width:1000px; padding-top:70px; overflow:hidden;">


<div style="width:600px; float:left;">
<div style=" height:40px; line-height:40px; color:#fff; font-size:16px; overflow:hidden; background:#6bb3f6; padding-left:20px;">网站防火墙 </div>
<div style="border:1px dashed #cdcece; border-top:none; font-size:14px; background:#fff; color:#555; line-height:24px; height:220px; padding:20px 20px 0 20px; overflow-y:auto;background:#f3f7f9;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600; color:#fc4f03;">您的请求带有不合法参数,已被网站管理员设置拦截!</span>


<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">可能原因:您提交的内容包含危险的攻击请求


<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:1; text-indent:0px;">如何解决:


<ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1)检查提交内容;</li>
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2)如网站托管,请联系空间提供商;</li>
<li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3)普通网站访客,请联系网站管理员;</li></ul>
</div>
</div>
</div>
</body></html>

root@ubuntu:/opt/nginx#

可以参考如下博客:
https://blog.csdn.net/m0_37814112/article/details/119967839?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-119967839-blog-106795279.pc_relevant_multi_platform_featuressortv2dupreplace&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-119967839-blog-106795279.pc_relevant_multi_platform_featuressortv2dupreplace&utm_relevant_index=2

点赞 0
浏览 4.4k