Load Balancing(NGINX) Help Needed.

October 6, 2013 4.8k views
Hi, I am working on Load Balancing for my websites... I got this: upstream backend { ny1 192.241.143.207; ny2 162.243.27.154; sf1 162.243.133.133; } The servers host like 7 sites each(all the same files)... What do I put in here: ny1 { location / { proxy_pass http://backend;
17 Answers
Your config looks proper except that
ny1 { 

location / {
proxy_pass http://backend; }
}
should perhaps be
server { 

location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
instead.
Hey Kamal!
What would I put for proxy_pass though? Why is http://backend in the tutorial? o.O
proxy_pass http://backend instructs nginx to proxy to the upstream that is called backend which you have defined in the first 5 lines.

It should also be
upstream backend { 

server 192.241.143.207;
server 162.243.27.154;
server 162.243.133.133;
}
instead of
upstream backend { 

ny1 192.241.143.207;
ny2 162.243.27.154;
sf1 162.243.133.133;
}
OH! Thanks. I will let you know how it goes.
I gave you a twitter shout out :)
I'm getting this:
root@load-balancer:~# service nginx reload
Reloading nginx configuration: nginx: [emerg] unknown directive "ny1" in /etc/nginx/sites-enabled/default:2
nginx: configuration file /etc/nginx/nginx.conf test failed
With this:
upstream backend {
ny1 192.241.143.207;
ny2 162.243.27.154;
sf1 162.243.133.133;
}

ny1 {
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}

ny2 {
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}

sf1 {
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
Do I need anything else other than that in my "default" file? It's all I got.
Oh wait! I'll take a gamble that ny1,ny2,sf1 needs to be server as in the tutorial. I thought it was placeholder.
You don't need all of these server blocks. You need only one upstream block and server block.
Nope:
upstream backend {
ny1 192.241.143.207;
ny2 162.243.27.154;
sf1 162.243.133.133;
}

server {
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}

You able to correct me? :/
New to NGINX kinda. :D I like experimenting.
Try this config instead: https://p.kk7.me/geruxucohu.nginx

Also, it's worth mentioning that this config should be on a separate nginx load balancer so you will have 4 servers in total.
I have 4 servers in total. And I sussed it out!
upstream backend {
server 192.241.143.207;
server 162.243.27.154;
server 162.243.133.133;
}

server {
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
Another thing now... how do I move ALL MySQL database records from my server1 to load-balancer(just so it's all organised how I love it) and remove it from server1,2,3? Thanks for the help so far!
by Etel Sverdlov
This article covers the process required to transfer MySQL data between two servers. This tutorial includes steps on how to perform a MySQL dump, transfer the database with SCP, and then reimport it on the second server.
You're very fast. IT'S OVAR 9000/10 from me. =]
That broke database connections,
http://stackoverflow.com/questions/19213973/pdo-cannot-connect-to-remote-mysql
Did you update your websites with the new location of the MySQL server?
Yes, updated some of them, and some still doing. :)
I managed to fix the pdo thing, I didn't have mysql installed so I had to run:
sudo apt-get install php5-mysql
to get the drivers.
Have another answer? Share your knowledge.