Nginx反向代理Keep-Alive设置

1.  环境说明

使用Nginx作为反向代理,示意图如下:

客户端与Nginx通信时,使用HTTPS协议;Nginx与服务器通信时,使用HTTP协议。

2.  Nginx文档说明

2.1  代理HTTP版本说明

查看Nginx文档,http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version,关于代理的HTTP协议版本的说明如下:

Syntax: proxy_http_version 1.0 | 1.1;

Default:   

proxy_http_version 1.0;

Context:    http, server, location

This directive appeared in version 1.1.4.

 

Sets the HTTP protocol version for proxying. By default, version 1.0 is used. Version 1.1 is recommended for use with keepalive connections and NTLM authentication.

proxy_http_version参数用于设置代理的HTTP协议版本,默认值为1.0。可通过将该参数设置为1.1,使代理使用HTTP 1.1版本。

proxy_http_version参数从Nginx 1.1.4版本开始出现。

2.2  代理Keep-Alive设置说明

查看Nginx文档,http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive,关于代理的Keep-Alive设置的说明如下:

Syntax: keepalive connections;

Default:    —

Context:    upstream

This directive appeared in version 1.1.4.

Activates the cache for connections to upstream servers.

 

The connections parameter sets the maximum number of idle keepalive connections to upstream servers that are preserved in the cache of each worker process. When this number is exceeded, the least recently used connections are closed.

 

It should be particularly noted that the keepalive directive does not limit the total number of connections to upstream servers that an nginx worker process can open. The connections parameter should be set to a number small enough to let upstream servers process new incoming connections as well.

For HTTP, the proxy_http_version directive should be set to “1.1” and the “Connection” header field should be cleared:

 

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 "";

        ...

    }

}

 

Alternatively, HTTP/1.0 persistent connections can be used by passing the “Connection: Keep-Alive” header field to an upstream server, though this method is not recommended.

2.3  使代理使用Keep-Alive的设置

根据以上说明可知,Nginx代理需要使用Keep-Alive时,需要进行以下设置:

l  将“proxy_http_version”参数设置为“1.1”(proxy_http_version 1.1);

l  将HTTP头的“Connection”参数清空(proxy_set_header Connection "")。

keepalive参数从Nginx 1.1.4版本开始出现。

3.  通信数据分析

在服务器进行抓包,分析Nginx与服务器之间的通信数据。

3.1  Nginx反向代理常见设置

Nginx的反向代理使用比较常见的配置,如下:

    server {

        listen       443 ssl;

        server_name  xxx.yyy.com;

 

        ssl_certificate      xxx;

        ssl_certificate_key  xxx;

        ssl_protocols xxx;

        ssl_ciphers xxx;

 

        location /w...a/ {

            proxy_pass   http://xxx:xxx/w...a/;

            proxy_redirect off;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header Host $http_host;

        }

 

    }

在客户端通过Nginx代理访问服务器,通信数据如下。

可以看到Nginx向服务器发送的HTTP请求中,HTTP协议版本为1.0;HTTP头中包含“Connection: close”,即不使用Keep-Alive。

3.2  设置HTTP协议版本为1.1

在Nginx的反向代理配置中,将HTTP协议版本设置为1.1,如下:

        location /w...a/ {

            proxy_pass   http://10.107.99.106:21843/w...a/;

            ...

            proxy_http_version 1.1;

        }

在客户端通过Nginx代理访问服务器,通信数据如下。

可以看到Nginx向服务器发送的HTTP请求中,HTTP协议版本为1.1;HTTP头中包含“Connection: close”,即不使用Keep-Alive。

3.3  使用upstream设置反向代理

在Nginx的反向代理配置中,将HTTP协议版本设置为1.1,并使用upstream设置反向代理,如下:

        upstream iwftscpa {

            server xxx:xxx;

            keepalive 100;

        }

 

        location /w...a/ {

            proxy_pass http://iwftscpa/w...a/;

            ...

            proxy_http_version 1.1;

        }

在客户端通过Nginx代理访问服务器,通信数据如下。

可以看到Nginx向服务器发送的HTTP请求中,HTTP协议版本为1.1;HTTP头中包含“Connection: close”,即不使用Keep-Alive。

3.4  清空HTTP头的Connection参数

在Nginx的反向代理配置中,将HTTP协议版本设置为1.1,使用upstream设置反向代理,并通过proxy_set_header命令将HTTP头的Connection参数清空,如下:

        upstream iwftscpa {

            server xxx:xxx;

            keepalive 100;

        }

 

        location /w...a/ {

            proxy_pass http://iwftscpa/w...a/;

            ...

            proxy_set_header Connection "";

            proxy_http_version 1.1;

        }

在客户端通过Nginx代理访问服务器,通信数据如下。

可以看到Nginx向服务器发送的HTTP请求中,HTTP协议版本为1.1;HTTP头中不包含“Connection: close”,HTTP 1.1默认使用Keep-Alive。

同时可以看到,在一次TCP连接中,发起了多次HTTP请求,可以确认Keep-Alive已生效。

3.5  结论

必须完全按照Nginx文档配置,才可以使Nginx代理与服务器通信时,使用Keep-Alive。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值