Nginx1.1.4+ 对后端机器的长连接特性
2015-06-09 07:27
585 查看
转自:http://zhangxugg-163-com.iteye.com/blog/1551269
nginx upstream keepalive connections
Nginx从 1.1.4 开始,实现了对后端机器的长连接支持,这是一个激动人心的改进,这意味着 Nginx 与后端机器的通信效率更高,后端机器的负担更低。
例如,对一个没有长连接支持的后端机器,会出现大量TIME_WAIT 状态的连接,使用以下命令验证之:
netstat -n | grep TIME_WAIT
经过查阅官方文档,其目前已经实现了http, fastcgi, memcache 协议的长连接支持。而之前的版本中仅支持 memcache 协议。
1. 启用到 memcache 服务器的长连接
在upstream
配置段中增加
keepalive N
指令即可:
upstream memcached_backend {
server 127.0.0.1:11211;
server 10.0.0.2:11211;
keepalive 32;
}
server {
...
location /memcached/ {
set $memcached_key $uri;
memcached_pass memcached_backend;
}
}
2.
启用fastcgi
长连接支持
除了需要在upstream
中配置
keepalive N
外,还需要在
location
中增加
fastcgi_keep_conn on;
upstream fastcgi_backend {
server 127.0.0.1:9000;
keepalive 8;
}
server {
...
location /fastcgi/ {
fastcgi_pass fastcgi_backend;
fastcgi_keep_conn on;
...
}
}
3.
启用对后端机器HTTP
长连接支持
upstream http_backend {
server 127.0.0.1:8080;
keepalive 16;
}
server {
...
location /http/ {
proxy_pass http://http_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
...
}
}
注意:需要设置nginx
代理请求的
http
协议版本号为
1.1,
以及清除掉
Connection
请求
header,
官方文档描述:
For HTTP, the proxy_http_version directive should be set to “
1.1
”
and the
“
Connection
”
header field should be cleared
.
The connections parameter should be set low enough to allow upstream servers to process additional new incoming connections as well.
即是说:keepalive N
指令中
, N
的值应该尽可能设置小一些,以便后端机器可以同时接受新的连接。
在我负责的生产环境中,前端是nginx,
静态文件缓存使用
varnish,
使用长连接之后,
varnish
机器的连接数从
8000
多下降至
200
多,负载值也有明显降低。
但是针对fastcgi,
即后端机器是
php-fpm
服务时,在
nginx
日志中出现以下错误:
upstream sent unsupported FastCGI protocol version: 0 while reading upstream
。
广泛搜集,目前还未解决之。如果您遇到同样的问题并解决之,请一定联系笔者信箱zhangxugg@163.com,
甚是感谢。
nginx upstream keepalive connections
Nginx从 1.1.4 开始,实现了对后端机器的长连接支持,这是一个激动人心的改进,这意味着 Nginx 与后端机器的通信效率更高,后端机器的负担更低。
例如,对一个没有长连接支持的后端机器,会出现大量TIME_WAIT 状态的连接,使用以下命令验证之:
netstat -n | grep TIME_WAIT
经过查阅官方文档,其目前已经实现了http, fastcgi, memcache 协议的长连接支持。而之前的版本中仅支持 memcache 协议。
1. 启用到 memcache 服务器的长连接
在upstream
配置段中增加
keepalive N
指令即可:
upstream memcached_backend {
server 127.0.0.1:11211;
server 10.0.0.2:11211;
keepalive 32;
}
server {
...
location /memcached/ {
set $memcached_key $uri;
memcached_pass memcached_backend;
}
}
2.
启用fastcgi
长连接支持
除了需要在upstream
中配置
keepalive N
外,还需要在
location
中增加
fastcgi_keep_conn on;
upstream fastcgi_backend {
server 127.0.0.1:9000;
keepalive 8;
}
server {
...
location /fastcgi/ {
fastcgi_pass fastcgi_backend;
fastcgi_keep_conn on;
...
}
}
3.
启用对后端机器HTTP
长连接支持
upstream http_backend {
server 127.0.0.1:8080;
keepalive 16;
}
server {
...
location /http/ {
proxy_pass http://http_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
...
}
}
注意:需要设置nginx
代理请求的
http
协议版本号为
1.1,
以及清除掉
Connection
请求
header,
官方文档描述:
For HTTP, the proxy_http_version directive should be set to “
1.1
”
and the
“
Connection
”
header field should be cleared
.
The connections parameter should be set low enough to allow upstream servers to process additional new incoming connections as well.
即是说:keepalive N
指令中
, N
的值应该尽可能设置小一些,以便后端机器可以同时接受新的连接。
在我负责的生产环境中,前端是nginx,
静态文件缓存使用
varnish,
使用长连接之后,
varnish
机器的连接数从
8000
多下降至
200
多,负载值也有明显降低。
但是针对fastcgi,
即后端机器是
php-fpm
服务时,在
nginx
日志中出现以下错误:
upstream sent unsupported FastCGI protocol version: 0 while reading upstream
。
广泛搜集,目前还未解决之。如果您遇到同样的问题并解决之,请一定联系笔者信箱zhangxugg@163.com,
甚是感谢。
相关文章推荐
- Nginx1.1.4+ 对后端机器的长连接特性
- NGINX轻松管理10万长连接 --- 基于2GB内存的CentOS 6.5 x86-64
- NGINX轻松管理10万长连接 --- 基于2GB内存的CentOS 6.5 x86-64
- 搭建heartbeat运行后发现nginx及VIP不能正常启动
- 实例配置keepalived+lvs+nginx
- Windows下安装Nginx
- nginx用第三方yum源atomic安装,与mysql数据库的安装步骤。atomic只用来安装nginx。
- 将ImageMagic库编译进nginx
- Nginx的Rewrite设置及示例
- OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能
- nginx访问控制配置
- NGINX关于配置PATHINFO
- linux 把nginx加入到系统服务,并开机自己启动的方法
- 简化的nginx多进程模型demo
- nginx+keepalived实现双机热备高可用
- 在linux下配置nginx+java+php的环境
- 搭建Nginx+Java环境
- Apache与Nginx的区别
- Apache与Nginx的区分比较
- nginx 安装配置 for window