Http upstream keepalives

Hi, I’m reading here:
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;
}
}

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

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 :slight_smile:

Thanks,
d.

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

[…]

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

What modification is needed?

It seems that the documentation is already clear enough:

Module ngx_http_proxy_module
“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

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

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 :slight_smile:

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. [email protected]

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):

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.