Thursday, February 19, 2009

practical guide to using nginx loadbalancer - Step 1

In this guide i will show you how to use nginx as an load balancer for php sites running on apache

You can see the old article where i describe the basic idea

I suppose nginx+apache are already installed and working
apache by default is on port 80 nginx on my machine is on port 8087 (by default is on 80 but i moved to 8087)




What is next ? Step one is to test nginx as an proxy for apache and to be an accelerator for static content
apache is to slow on that area , and nginx/lighty are small and blazing fast
replace any 80 port in apache with 8000
grep -r 80 /etc/apache2

and wherever you see 80 in port.conf or virtual host please replace with 8000

hen i have created the proxy config
sudo vi /etc/nginx/sites-enabled/proxy


server {
listen: 80;
server_name: foobar.example.com;
location / {
proxy_pass http://foobar.example.comt:8000;
}
}

restart both apache and nginx

and you should load the local websites and test if they are ok

Next we will add an rule so that all images / javascript /css will not be proxied but served by nginx
erver {
listen 80;
server_name www.foobar.example.comt;
# serve static files
location ~ ^/(img|images|javascript|js|css|flash|media|static)/ {
root /var/www;
# expires 30d;
}
location / {
proxy_pass http://www.foobar.example.com:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}


So now we have fast serving for images and php is served by one apache
Next we will add more apache servers and do some tests


No comments: