# Http upstream keepalives

**URL:** <https://www.ruby-forum.com/t/http-upstream-keepalives/219744>\
**Category:** NGINX\
**Created:** [May 14, 2012, 7:32pm UTC](https://www.ruby-forum.com/t/http-upstream-keepalives/219744 "2012-05-14T19:32:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 14, 2012, 7:32pm UTC](https://www.ruby-forum.com/t/http-upstream-keepalives/219744/1 "2012-05-14T19:32:40Z")

</div>

Hi, I’m reading here:  
[http://nginx.org/en/docs/http/ngx\_http\_upstream\_module.html#keepalive](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive)

so I’ve tried:

server {  
keepalive\_timeout 70;  
error\_log /var/log/nginx/test-error.log;

listen 80;  
server\_name www.dave.it;  
proxy\_read\_timeout 3600;

proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;

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

On 172.16.7.9 I launched:

> nc -4 -l 172.16.7.9 2323

And so I saw:  
GET / HTTP/1.1  
Host: 172.16.7.9:2323  
User-Agent: Wget/1.12 (darwin10.5.0)  
Accept: _/_

As you can see the Host header is wrong so I tested this vhost:  
server {  
keepalive\_timeout 70;  
error\_log /var/log/nginx/test-error.log;

listen 80;  
server\_name www.dave.it;  
proxy\_read\_timeout 3600;

proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_http\_version 1.1;  
proxy\_set\_header Connection “”;

location / {  
proxy\_pass [http://172.16.7.9:2323](http://172.16.7.9:2323);  
}  
}

And this time the backend answered as expected:

GET / HTTP/1.1  
Host: www.dave.it  
X-Real-IP: 172.16.7.249  
X-Forwarded-For: 172.16.7.249  
User-Agent: Wget/1.12 (darwin10.5.0)  
Accept: _/_

So I think that a little modification to documentation could be a good  
idea 🙂

Thanks,  
d.

---

<div class="post-metadata">

**Author:** ![Davide\_DSAmico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Davide\_DSAmico](https://www.ruby-forum.com/u/Davide_DSAmico)\
**Post date:** [May 14, 2012, 8:26pm UTC](https://www.ruby-forum.com/t/http-upstream-keepalives/219744/2 "2012-05-14T20:26:44Z")

</div>

On Monday 14 May 2012 21:32:18 Davide D’Amico wrote:

> server\_name www.dave.it;  
> proxy\_pass [http://172.16.7.9:2323](http://172.16.7.9:2323);  
> }  
> }

[…]

> So I think that a little modification to documentation could be a good  
> idea 🙂

What modification is needed?

It seems that the documentation is already clear enough:

[Module ngx\_http\_proxy\_module](http://nginx.org/r/proxy_set_header)  
“These directives are inherited from the previous level if and only if  
there are no proxy\_set\_header directives defined on the current  
level.”

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Davide\_DSAmico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Davide\_DSAmico](https://www.ruby-forum.com/u/Davide_DSAmico)\
**Post date:** [May 14, 2012, 8:57pm UTC](https://www.ruby-forum.com/t/http-upstream-keepalives/219744/3 "2012-05-14T20:57:46Z")

</div>

On Mon, May 14, 2012 at 07:32:18PM +0200, Davide D’Amico wrote:

Hi there,

> Hi, I’m reading here:  
> [Module ngx\_http\_upstream\_module](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive)
> 
> so I’ve tried:
> 
> server {

> proxy\_set\_header Host $host;  
> proxy\_set\_header X-Real-IP $remote\_addr;  
> proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;

> location / {  
> proxy\_http\_version 1.1;  
> proxy\_set\_header Connection “”;

That proxy\_set\_header directive means that any at an enclosing scope  
are not relevant in this location{}.

> Host: 172.16.7.9:2323  
> User-Agent: Wget/1.12 (darwin10.5.0)  
> Accept: _/_
> 
> As you can see the Host header is wrong so I tested this vhost:

No, the Host: header is what you configured it to be.

As your next example also shows.

> So I think that a little modification to documentation could be a good  
> idea 🙂

The documentation you link to looks correct to me.

(As I read it, it says “Last-Modified: Mon, 23 Apr 2012 13:32:31 GMT”.)

It includes “…” where you include specific directives. It turns out  
that  
the specific directives you include don’t do what you expected them to.

So: what documentation update could have avoided your confusion, or  
adjusted your expectations to match what nginx actually does?

Note that this is unrelated to the “keepalive” directive; it is arguably  
related to the proxy\_set\_header directive; but it is probably more  
generally related to directive inheritance in nginx.

Some directives don’t inherit at all – so if you want it to apply in  
a location{}, you must set it in that location.

Some directives do inherit from http \> server \> location – so to see  
whether it applies in a location{}, you may have to check enclosing  
scopes too.

Some (few) directives are “paired”, and will affect the inheritance of a  
different directive – so to see whether it applies in a location{}, you  
have to check its partner directive too, possibly in enclosing scopes.

But for all(?) directives that do inherit, inheritance is by  
replacement,  
not addition.

Where would you have looked to find notes like the above? Perhaps if a  
suitable place can be identified, a corrected version could be put  
there.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Davide\_DSAmico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Davide\_DSAmico](https://www.ruby-forum.com/u/Davide_DSAmico)\
**Post date:** [May 15, 2012, 11:29pm UTC](https://www.ruby-forum.com/t/http-upstream-keepalives/219744/4 "2012-05-15T23:29:27Z")

</div>

> On Mon, May 14, 2012 at 07:32:18PM +0200, Davide D’Amico wrote:
> 
> are not relevant in this location{}.
> 
> > GET / HTTP/1.1
> 
> that
> 
> you  
> there.

Indeed you are right but I read  
([Module ngx\_http\_upstream\_module](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive)):

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

```

}

So i was thinking that in this scenario all other proxy\_set\_header  
options were inherited from the server stanza.  
Clarify this point could help. That’s all.

Thanks for your answers.
