# How to use keepalive with Nginx revers proxy?

**URL:** <https://www.ruby-forum.com/t/how-to-use-keepalive-with-nginx-revers-proxy/240215>\
**Category:** NGINX\
**Created:** [April 2, 2014, 3:26pm UTC](https://www.ruby-forum.com/t/how-to-use-keepalive-with-nginx-revers-proxy/240215 "2014-04-02T15:26:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [April 2, 2014, 3:26pm UTC](https://www.ruby-forum.com/t/how-to-use-keepalive-with-nginx-revers-proxy/240215/1 "2014-04-02T15:26:42Z")

</div>

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 ;

Thanks,  
Makaikol

---

<div class="post-metadata">

**Author:** ![Makailol\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@Makailol\_C](https://www.ruby-forum.com/u/Makailol_C)\
**Post date:** [April 2, 2014, 4:00pm UTC](https://www.ruby-forum.com/t/how-to-use-keepalive-with-nginx-revers-proxy/240215/2 "2014-04-02T16:00:35Z")

</div>

Hello!

On Wed, Apr 02, 2014 at 06:55:42PM +0530, Makailol C. 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:

> **[Module ngx\_http\_upstream\_module](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive)**
>
> The ngx\_http\_upstream\_module module is used to define groups of servers that can be referenced by the proxy\_pass, fastcgi\_pass, uwsgi\_pass, scgi\_pass, memcached\_pass, and grpc\_pass directives.

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 D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![Makailol\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@Makailol\_C](https://www.ruby-forum.com/u/Makailol_C)\
**Post date:** [April 3, 2014, 6:02am UTC](https://www.ruby-forum.com/t/how-to-use-keepalive-with-nginx-revers-proxy/240215/3 "2014-04-03T06:02:15Z")

</div>

Hi Maxim,

Thanks for reply.

Number of IPs are not fixed so it is not possible to define upstream and  
map block I think. I am trying to implement completely dynamic  
configuration using lua module.

Is it possible to use variable in upstream block like this?  
upstream backend {  
server $IP;  
keepalive 2;  
}

location / {  
proxy\_pass [http://backend](http://backend);  
proxy\_http\_version 1.1;  
proxy\_set\_header Connection “”;  
}

Thanks,  
Makailol

---

<div class="post-metadata">

**Author:** ![Makailol\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@Makailol\_C](https://www.ruby-forum.com/u/Makailol_C)\
**Post date:** [December 14, 2015, 3:27am UTC](https://www.ruby-forum.com/t/how-to-use-keepalive-with-nginx-revers-proxy/240215/4 "2015-12-14T03:27:52Z")

</div>

I have the same question, any update on it. I am using Lua and Redis for  
the  
reverse proxy lookup and don’t define upstream. How can we use  
keepalive?

Posted at Nginx Forum:

> **[Re: how to use keepalive with Nginx revers proxy?](https://forum.nginx.org/read.php?2,248924,263444#msg-263444)**
>
> 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 hav

---

<div class="post-metadata">

**Author:** ![Makailol\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@Makailol\_C](https://www.ruby-forum.com/u/Makailol_C)\
**Post date:** [April 3, 2014, 3:02pm UTC](https://www.ruby-forum.com/t/how-to-use-keepalive-with-nginx-revers-proxy/240215/5 "2014-04-03T15:02:58Z")

</div>

Hello!

On Thu, Apr 03, 2014 at 09:31:08AM +0530, Makailol C. wrote:

> upstream backend {  
> server $IP;  
> keepalive 2;  
> }

No, this won’t work.

> > > Nginx(reverse proxy) and backend server?  
> > > [Module ngx\_http\_upstream\_module](http://nginx.org/r/keepalive)  
> > > …  
> > > }
> > 
> > –  
> > Maxim D.  
> > [http://nginx.org/](http://nginx.org/)
> > 
> > * * *
> > 
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![Makailol\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@Makailol\_C](https://www.ruby-forum.com/u/Makailol_C)\
**Post date:** [December 14, 2015, 5:29pm UTC](https://www.ruby-forum.com/t/how-to-use-keepalive-with-nginx-revers-proxy/240215/6 "2015-12-14T17:29:40Z")

</div>

Hello!

On Sun, Dec 13, 2015 at 09:28:27PM -0500, winghokwan wrote:

> I have the same issue. I use Lua and Redis for the reverse proxy lookup and  
> don’t use upstream. How to use keepalive then?

The only way now is to define upstream{} blocks in advance, and  
then provide appropriate upstream{} block name from your dynamic  
lookup code.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![Makailol\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71c47a/32.png) [@Makailol\_C](https://www.ruby-forum.com/u/Makailol_C)\
**Post date:** [December 14, 2015, 3:29am UTC](https://www.ruby-forum.com/t/how-to-use-keepalive-with-nginx-revers-proxy/240215/7 "2015-12-14T03:29:03Z")

</div>

I have the same issue. I use Lua and Redis for the reverse proxy lookup  
and  
don’t use upstream. How to use keepalive then?

Posted at Nginx Forum:

> **[Re: how to use keepalive with Nginx revers proxy?](https://forum.nginx.org/read.php?2,248924,263445#msg-263445)**
>
> 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 hav
