basic nginx setup help as load balancer

Reinis Rozitis r at roze.lv
Tue Jul 5 18:42:18 UTC 2016


> But I am confused on a few concepts here. First of all, should my server 
> name in the "upstream" directive be the same name in the "server_name" 
> directive in the "server" stanza? Here is what I have so far:

> And to recap, should my server name in the "upstream" directive be the 
> same name in the "server_name" directive in the "server" stanza?


It is not a requirement, but depending on how your backend servers are 
configured (if they are namebased virtualhosts) you may need to specify 
correct Host header.


By default  nginx sends whatever it is given in the proxy_pass directive.

Taking your configuration for example:

upstream myapplication.net {
        server 1.net;
        server 2.net;
        server 3..net;
}

location {
    proxy_pass   http://myapplication.net;
}


1. On startup Nginx will resolve the 1.net .. 3.net hostnames
2. Will send to whatever upstream server IP (not using the upstream 
hostsnames) it has chosen a request for 'myapplication.net' (Host). It also 
doesn't use server_name.

If the backend has a namebased configuration and there is no 
'myapplication'net' virtualhost (or it isnt the default one) the whole 
request will genereally fail (not return what was expected).

If that is the case you either need to configure the upstream block (which 
for nginx is just a virtual name) and the proxy_pass to match your backend 
configuration or usually people just add additional header:

location {
    proxy_pass   http://myapplication.net;
    proxy_set_header Host  $host;
}

This way nginx sends to backend the actual hostname from request.

Of course you can use also $server_name (or any other variable 
http://nginx.org/en/docs/varindex.html or even static value) but usually 
server_name is something like .domain.com (for wildcard matching) so it may 
confuse the backend.

rr



More information about the nginx mailing list