# Proxy non persistent client connections to persistent upstream connections

**URL:** <https://www.ruby-forum.com/t/proxy-non-persistent-client-connections-to-persistent-upstream-connections/242951>\
**Category:** NGINX\
**Created:** [August 10, 2015, 10:19am UTC](https://www.ruby-forum.com/t/proxy-non-persistent-client-connections-to-persistent-upstream-connections/242951 "2015-08-10T10:19:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [August 10, 2015, 10:19am UTC](https://www.ruby-forum.com/t/proxy-non-persistent-client-connections-to-persistent-upstream-connections/242951/1 "2015-08-10T10:19:04Z")

</div>

I would like to be able to proxy non persistent client http connections  
to  
persistent upstream connections on a Linux system, both to reduce the  
number of connections and the upstream latency.

I have experimented with keepalive and proxy\_pass.

To observe performance I used log\_format upstreamlog ‘[$time\_local]  
$status  
$connection $connection\_requests $request\_time’; and $ lsof -Pni

What I think I have found is that some of the time persistent upstream  
connections are reused, but very often they are not. I believe the  
occasions where upstream connections are reused are possible because by  
chance the client connection has been reused.

Is there a way to make this work?

Regards

Example configuration

log\_format upstreamlog ‘[$time\_local] $status $connection  
$connection\_requests $request\_time’;

upstream google {  
server [google.com:80](http://google.com:80);  
keepalive 10;  
}

upstream yahoo {  
server [yahoo.com:80](http://yahoo.com:80)  
keepalive 10;  
}

server {  
listen 8080;  
server\_name 10.0.0.1;  
proxy\_connect\_timeout 500ms;  
proxy\_send\_timeout 500ms;  
proxy\_read\_timeout 500ms;  
send\_timeout 500ms;

```
location /status {
    stub_status on;
}

location /google {
    proxy_pass http://www.google.com;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header Host google.com;
   access_log /var/log/google.log upstreamlog;
}

location /yahoo {
    proxy_pass http://www.yahoo.com;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header Host yahoo.com;
   access_log /var/log/yahoo.log upstreamlog;
}

```

}

---

<div class="post-metadata">

**Author:** ![Richard\_Jennings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b3f665/32.png) [@Richard\_Jennings](https://www.ruby-forum.com/u/Richard_Jennings)\
**Post date:** [August 11, 2015, 2:34pm UTC](https://www.ruby-forum.com/t/proxy-non-persistent-client-connections-to-persistent-upstream-connections/242951/2 "2015-08-11T14:34:28Z")

</div>

Hello!

On Mon, Aug 10, 2015 at 08:18:41AM +0000, Richard Jennings wrote:

> connections are reused, but very often they are not. I believe the  
> occasions where upstream connections are reused are possible because by  
> chance the client connection has been reused.
> 
> Is there a way to make this work?

In nginx, client connections and upstream connections are not  
related to each other and can be kept alive separately. That is, what  
you are trying to do is how it works by design.

It doesn’t work because of an error in your config: you proxy to

> ```
> proxy_pass http://www.google.com;
> 
> ```

and

> ```
> proxy_pass http://www.yahoo.com;
> 
> ```

but your upstream blocks are called “google” and “yahoo”  
respectively. You should use

```
proxy_pass http://google;

```

and

```
proxy_pass http://yahoo;

```

instead (or rename “upstream” blocks accordingly).

–  
Maxim D.

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