The previous article says about the load balancing of LVS: http://1015489314.blog.51cto.com/8637744/1700710
This article is about Nginx load balancing, which is actually the same function as the Nginx proxy, but instead of acting on a machine to proxy more than one machine. Nginx load balancer compared to LVS, Nginx belongs to a more advanced application layer, not involved in IP and kernel changes, it simply forward the user's request to the back of the machine. This means that the rear-end RS does not need to configure the public IP.
One, Nginx proxy implementation cluster
1. Preparatory work
Prepare three machines.
Nginx Dispatcher (a public network ip192.168.1.109 and an intranet ip192.168.0.109).
Rs1 only intranet IP (192.168.0.112)
RS2 only intranet IP (192.168.0.113)
At the same time, three machines have to install Nginx, and open.
# yum Install-y nginx//If not, you can install the Epel extension source first: Epel-release
#/etc/init.d/nginx Start
2. Configuration
Editing the configuration file on the Nginx dispatcher
[Email protected] ~]# vim/usr/local/nginx/conf/vhosts/lb.conf//Add the following:
Upstream Test {
Ip_hash;
Server 192.168.0.112;
Server 192.168.0.113;
}
server {
Listen 80;
server_name www.tpp.com;
Location/{
Proxy_pass http://test/;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}
Re-start Nginx
[Email protected] ~]#/etc/init.d/nginx restart
Configuring on Rs1 and RS2
We modify the default page of Nginx
Rs1 on
echo "Rs1 nginx" >/usr/share/nginx/html/index.html
RS2 on
echo "Rs2 Nginx" >/usr/share/nginx/html/index.html
3. Testing
First, rs1 and RS2 are shutting down the firewall.
# iptables-f
Then execute the test command on the Nginx dispatcher
[Email protected] ~]# curl-xlocalhost:80 www.tpp.com
Rs1 Nginx
[Email protected] ~]# curl-xlocalhost:80 www.tpp.com
RS2 Nginx
[Email protected] ~]# curl-xlocalhost:80 www.tpp.com
Rs1 Nginx
[Email protected] ~]# curl-xlocalhost:80 www.tpp.com
RS2 Nginx
[Email protected] ~]# curl-xlocalhost:80 www.tpp.com
Rs1 Nginx
[Email protected] ~]# curl-xlocalhost:80 www.tpp.com
RS2 Nginx
Second, based on the directory accessed to differentiate the backend web
Requirement: When the requested directory is/aaa/the request is sent to the machine rs1, except for the directory/aaa/, other requests are sent to the machine rs2.
Configure on the Dispenser
Upstream Testa {
Server 192.168.0.112;
}
Upstream Testb {
Server 192.168.0.113;
}
server {
Listen 80;
server_name www.tpp.com;
location/aaa/{
Proxy_pass http://testa/aaa/;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
Location/{
Proxy_pass http://testb/;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}
Attention:
1) The test, Testa, and Testb in the above configuration files are custom and write-free.
2) Proxy_pass http://testa/aaa/This directory must be added here, or you will have access to the root directory.
3) based on the directory accessed to distinguish the backend of the web, this LVS can not be done.
This article is from the "M April Days" blog, please be sure to keep this source http://1015489314.blog.51cto.com/8637744/1701337
The Nginx of LB cluster