# Basic nginx setup help as load balancer

**URL:** <https://www.ruby-forum.com/t/basic-nginx-setup-help-as-load-balancer/244903>\
**Category:** NGINX\
**Created:** [July 5, 2016, 6:12pm UTC](https://www.ruby-forum.com/t/basic-nginx-setup-help-as-load-balancer/244903 "2016-07-05T18:12:29Z")\
**Posts on this page:** 6\
**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:** [July 5, 2016, 6:12pm UTC](https://www.ruby-forum.com/t/basic-nginx-setup-help-as-load-balancer/244903/1 "2016-07-05T18:12:29Z")

</div>

I was getting a host not found problem while trying to start nginx.

nginx: [emerg] host not found in upstream “[backendservers.com](http://backendservers.com)” in  
/etc/nginx/conf.d/default.conf:37

I am fairly certain that this is because there is no name resolution for  
"  
[backendservers.com](http://backendservers.com)" in our DNS. So I changed up a few things to make it  
work.

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:

upstream [myapplication.net](http://myapplication.net) {  
# Use ip hash for session persistance  
ip\_hash;  
server [1.net](http://1.net);  
server [2.net](http://2.net);  
server 3…net;

```
    # The below only works on nginx plus
    #sticky route $route_cookie $route_uri;

```

}  
server {

```
listen 80;
server_name myapplication.net;
keepalive_timeout 70;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
    proxy_pass http://myapplication.net;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

 # include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}

```

}

server {

```
listen 443 ssl;
server_name myapplication.net;
keepalive_timeout 70;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
   proxy_pass https://myapplication.net;
   proxy_http_version 1.1;
   proxy_set_header Connection "";
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME

```

/scripts$fastcgi\_script\_name;  
# include fastcgi\_params;  
#}

```
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}

```

So here is what I need to happen. I need nginx to asnwer requests for  
[myapplication.net](http://myapplication.net) and send them to the servers “server1,net,  
[server2.net](http://server2.net),  
and [server3.net](http://server3.net)”. Am I accomplishing this with this config? And to  
recap,  
should my server name in the “upstream” directive be the same name in  
the  
“server\_name” directive in the “server” stanza?

---

<div class="post-metadata">

**Author:** ![NdridCold\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea5d25/32.png) [@NdridCold\_S](https://www.ruby-forum.com/u/NdridCold_S)\
**Post date:** [July 5, 2016, 8:42pm UTC](https://www.ruby-forum.com/t/basic-nginx-setup-help-as-load-balancer/244903/2 "2016-07-05T20:42:37Z")

</div>

> 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](http://myapplication.net) {  
server [1.net](http://1.net);  
server [2.net](http://2.net);  
server 3..net;  
}

location {  
proxy\_pass [http://myapplication.net](http://myapplication.net);  
}

1. On startup Nginx will resolve the [1.net](http://1.net) .. [3.net](http://3.net) hostnames
2. Will send to whatever upstream server IP (not using the upstream  
hostsnames) it has chosen a request for ‘[myapplication.net](http://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](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  
[Alphabetical index of variables](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

---

<div class="post-metadata">

**Author:** ![NdridCold\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea5d25/32.png) [@NdridCold\_S](https://www.ruby-forum.com/u/NdridCold_S)\
**Post date:** [July 5, 2016, 11:05pm UTC](https://www.ruby-forum.com/t/basic-nginx-setup-help-as-load-balancer/244903/3 "2016-07-05T23:05:10Z")

</div>

Thank you. In my setup all 3 servers in the upstream block will answer  
requests for “[myapplication.net](http://myapplication.net)” . Knowing that, would you say my config  
I  
have is sufficient?

---

<div class="post-metadata">

**Author:** ![NdridCold\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea5d25/32.png) [@NdridCold\_S](https://www.ruby-forum.com/u/NdridCold_S)\
**Post date:** [July 6, 2016, 9:05am UTC](https://www.ruby-forum.com/t/basic-nginx-setup-help-as-load-balancer/244903/4 "2016-07-06T09:05:48Z")

</div>

* * *

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

---

<div class="post-metadata">

**Author:** ![NdridCold\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea5d25/32.png) [@NdridCold\_S](https://www.ruby-forum.com/u/NdridCold_S)\
**Post date:** [July 6, 2016, 1:02pm UTC](https://www.ruby-forum.com/t/basic-nginx-setup-help-as-load-balancer/244903/5 "2016-07-06T13:02:58Z")

</div>

> I’m new to servers and proxies,  
> But don’t you think running both nginx and Apache on port 80 of same machine  
> will cause one of those to fail to start.  
> In my opinion backend should be on different IP:port combination.  
> Please correct me if I’m wrong.

It is correct (though you can work arround it by backend (apache)  
listening only on 127.0.0.1 interface and nginx as frontend on the real  
ip).

But to me the initial posters configuration excerpt looked like having  
just generic comments not representing the actual case.

rr

---

<div class="post-metadata">

**Author:** ![NdridCold\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ea5d25/32.png) [@NdridCold\_S](https://www.ruby-forum.com/u/NdridCold_S)\
**Post date:** [July 6, 2016, 12:56pm UTC](https://www.ruby-forum.com/t/basic-nginx-setup-help-as-load-balancer/244903/6 "2016-07-06T12:56:33Z")

</div>

> Thank you. In my setup all 3 servers in the upstream block will answer  
> requests for “[myapplication.net](http://myapplication.net)” . Knowing that, would you say my config I  
> have is sufficient?

It should be yes.

rr
