nginx upstream如何开启keepalive
upstream tomcat { server ops-coffee.cn:8080; keepalive 1024; } server { location / { proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass http://tomcat; } }
nginx在项目中大多数情况下会作为反向代理使用,例如nginx后接tomcat,nginx后接php等,这时我们开启nginx和后端服务之间的keepalive能够减少频繁创建TCP连接造成的资源消耗,配置如上
keepalive: 指定每个nginxworker可以保持的最大连接数量为1024,默认不设置,即nginx作为client时keepalive未生效
proxy_http_version 1.1: 开启keepalive要求HTTP协议版本为HTTP 1.1
proxy_set_header Connection "": 为了兼容老的协议以及防止http头中有Connection close导致的keepalive失效,这里需要及时清掉HTTP头部的Connection