Basic nginx setup help as load balancer

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

nginx: [emerg] host not found in upstream “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" 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 {
# Use ip hash for session persistance
ip_hash;
server 1.net;
server 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 and send them to the servers “server1,net,
server2.net,
and 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?

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.net3.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
Alphabetical index of variables or even static value) but usually
server_name is something like .domain.com (for wildcard matching) so it
may
confuse the backend.

rr

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


nginx mailing list
[email protected]
http://mailman.nginx.org/mailman/listinfo/nginx

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

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

It should be yes.

rr