nginx的upstream里不能支持长连接的排查

最近在对比apache和nginx放jboss前面的性能。发现我配置的nginx 对后端server不能使用长连接去连。具体的就是抓包吧:

tcpdump -i lo port 7002 and ‘tcp[tcpflags] & (tcp-syn|tcp-fin) !=0 ‘ -nn

可以看到每个请求完毕后连接都关闭了。
然后抓包搞一晚上,试过N个版本排查了N久,后恰好看文景同学在线就咨询了一下他。我之前的配置文件是:

[text]
upstream jboss {
server 127.0.0.1:7002;
keepalive 10;
}
………….
proxy_set_header Connection "";
proxy_http_version 1.1;
sendfile on;
rewrite ^$ /index.html permanent;
location / {
index index.html index.htm;
proxy_pass http://jboss;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-By $server_addr:$server_port;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_intercept_errors on;
proxy_connect_timeout 5s;
proxy_read_timeout 10s;
proxy_send_timeout 5s;
proxy_buffer_size 16k;
proxy_buffers 8 64k;
proxy_busy_buffers_size 128k;

}
[/text]

文景提醒我说proxy_set_header需要配置在一起才行,试过把 proxy_set_header Connection “”;放到location里面后果然就OK了。
当然,也可以把这些都直接放在server段,但是注意不要再在location短去设置httpd头.WIKI上的说明是
proxy_set_header directives issued at higher levels are only inherited when no proxy_set_header directives have been issued at a given level.

http://wiki.nginx.org/HttpProxyModule#proxy_set_header

此条目发表在nginx分类目录。将固定链接加入收藏夹。

发表回复