Opened 11 years ago

Closed 11 years ago

Last modified 7 years ago

#313 closed defect (invalid)

Issue with load balance several Apache instances.

Reported by: Chamith Malinda Owned by:
Priority: major Milestone:
Component: nginx-core Version: 1.1.x
Keywords: load balancing Cc: chamith.malinda@…
uname -a: Linux chamith-HP-Pavilion-g6-Notebook-PC 3.2.0-24-generic-pae #39-Ubuntu SMP Mon May 21 18:54:21 UTC 2012 i686 i686 i386 GNU/Linux
nginx -V: TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-dav-ext-module

Description

Hi,

I tried to load balance two Apache instances (virtual hosts) In two different physical servers.
Each one having more than one Apache virtual hosts with different domain names.

Let say there are two physical servers called "A" and "B",

Physical machine "A" have Apache virtual hosts with following domain names.

  1. exampleA1.com
  2. exampleA2.com

etc . . .

Physical machine "B" have Apache virtual hosts with following domain names.

  1. exampleB1.com
  2. exampleB2.com

etc . . .

Those Apache virtual hosts are working fine.

And I need to load balance exampleA1.com and exampleB1.com. So I used nginx for this with following configuration in my Local PC (for test before putting to a live server)

My target is load balance exampleA1.com and exampleB1.com from my local PC.

Following is my nginx.conf modifications,

http {

upstream localhost {

server exampleA1.com:80;
server exampleB1.com:80;

}

server {

listen localhost:80;
server_name localhost;

location / {
proxy_pass http://localhost;
}
}

}

So then when accessing http://localhost load balancing need to be happen.
And yes It's happening. But one problem.

It's picking other virtual host (Domain name) in the "A" physical machine. Instead of "exampleA1.com" it's picking "exampleA2.com" or any other one. In the "B" machine the same problem.

(When use browser to access those Apache URL 's - Working fine - directing to the correct destination)

Can't we balance the load two Apache instances in different PC s which are having more than one virtual hosts or Apache instances with different domain names ?


Apache configurations for exampleA1.com and exampleA2.com in "A" server like as follows,

<VirtualHost *:80>

ServerAdmin xxxx@…
DocumentRoot /var/www/
ServerName exampleA1.com

-----

</VirtualHost>

<VirtualHost *:80>

ServerAdmin xxxx@…
DocumentRoot /var/www/
ServerName exampleA2.com

------

</VirtualHost>

Apache configurations for exampleB1.com and exampleB2.com in "B" server like as follows,

<VirtualHost *:80>

ServerAdmin xxxx@…
DocumentRoot /var/www/
ServerName exampleB1.com

-----

</VirtualHost>

<VirtualHost *:80>

ServerAdmin xxxx@…
DocumentRoot /var/www/
ServerName exampleB2.com

------

</VirtualHost>

Thanks,
Chamith.

Change History (5)

comment:1 by Maxim Dounin, 11 years ago

Resolution: invalid
Status: newclosed

Balancing within single upstream block assumes the same request might be routed to any of the servers configured, much like things happen with multiple ip addresses configured for a domain name. Unless overridden with proxy_set_header directive, name you wrote in proxy_pass will be used in a Host header, and you have to configure virtual host "localhost" (== name you wrote in proxy_pass) on both of your backends for things to work.

in reply to:  1 comment:2 by Chamith Malinda, 11 years ago

Thanks for the explanation.

"you have to configure virtual host "localhost" (== name you wrote in proxy_pass) "

Is this name is "proxy pass" or "server_name" ?

Because If I put proxy_pass ​http://cluster; and put the cluster as the servername of two Virtual hosts in back end It's not working.

http {
upstream cluster {
server exampleA1.com:80;
server exampleB1.com:80;
}
server {
listen localhost:80;
server_name localhost;
location / {
proxy_pass ​http://cluster;
}
}
}

If I put localhost then It's working. Then we have to put the same domain name for two Apache Virtual hosts. Is that correct ?

comment:3 by Maxim Dounin, 11 years ago

You have to use identical names on two backend's virtual hosts (i.e., in your Apache configs, either using ServerName or ServerAlias directives in Apache configs). And you have to use the same name in nginx's proxy_pass (or, alternatively, use proxy_set_header Host with the name).

comment:4 by Chamith Malinda, 11 years ago

Great Thanks.

comment:5 by Maxim Dounin, 7 years ago

sensitive: 10
Note: See TracTickets for help on using tickets.