Nginx Upstream Keepalive配置

来源:互联网 发布:通州梨园 淘宝城 清退 编辑:程序博客网 时间:2024/06/05 18:50

Nginx 1.1.14版本以前与后端upstream服务器建立的都是短链接,即通过HTTP/1.0向后端发起连接,并把请求的”Connection” header设为”close”。这样nginx往upstream后端发请求时,也会消耗很多的时间与带宽,如果让nginx与upstream后端建立起长链接,从nginx发起的请求就可以挑选一个合适的长链接发往upstream后端服务器,这样即可以节省带宽,也可以提高响应速度。

针对后端的keepalive是通过nginx.conf配置文件来指定的,典型配置如下:

[php] view plain copy
print?
  1. upstream  httpd {  
  2.     server 172.189.85.156:1080;  
  3.     keepalive 1024;  
  4. }  
  5.   
  6. server {  
  7.     location / {  
  8.         proxy_set_header Connection ”“;  
  9.         proxy_http_version 1.1;  
  10.         proxy_set_header Host      $http_host;  
  11.         proxy_pass http://httpd;  
  12.     }  
  13. }  
upstream  httpd {    server 172.189.85.156:1080;    keepalive 1024;}server {    location / {        proxy_set_header Connection "";        proxy_http_version 1.1;        proxy_set_header Host      $http_host;        proxy_pass http://httpd;    }}

其中keepalive指定最大保持的长连接数为1024.,同时要求HTTP协议版本指定为HTTP 1.1,以及清除掉HTTP头部Connection


0 0
原创粉丝点击