how to use keepalive with Nginx revers proxy?

Maxim Dounin mdounin at mdounin.ru
Wed Apr 2 13:59:53 UTC 2014


Hello!

On Wed, Apr 02, 2014 at 06:55:42PM +0530, Makailol Charls wrote:

> Hi,
> 
> Can some one provide me an example to set keep alive connection between
> Nginx(reverse proxy) and backend server?
> 
> I can not use upstream module as my backend IP is dynamic based on
> variable. So I can not use keepalive directive of upstream.
> 
> I have used below directive in location block.
> proxy_pass http://$IP ;

Use of keepalive connections require upstream{} block to be 
defined, see here for examples:

http://nginx.org/r/keepalive

As long as list of backend ip addresses is limited, you may define 
appropriate upstream{} blocks for each backend, and use upstream's 
name in a variable, e.g.:

    upstream backend1 {
        server 192.168.0.1;
        keepalive 2;
    }

    ...

    map $IP $backend {
        192.168.0.1  backend1;
        ...
    }

    location / {
        proxy_pass http://$backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list