Nginx Reverse Proxy Server Load balancer cluster and nginx Server Load balancer Cluster
Nginx reverse proxy load balancing cluster practice
Using Nginx as a server Load balancer and proxy web server, the data requested by the user is directed to the Nginx Server Load balancer, and Nginx is responsible for scheduling backend Web servers to provide services.
Environment setup and description:
Nginx Load balancer LNMP environment or only install nginx service; two NICs, 192.168.11.30 (Public ip Simulation), 192.168.20.30 (Intranet)
Web Server LAMP environment 1: IP Address: Intranet 192.168.20.10 apache version 2.4
Web Server LAMP environment 2: IP address 192.168.20.11
Web Server LAMP environment 3: IP address 192.168.20.12
The three web server website directories are consistent with the program. The Intranet ip address must be in the same network segment as the nginx Server Load balancer.
1. Only one site is configured:
Web1, web2, and web3:
Add the httpd configuration file to allow access to the website directory; enable the vhost virtual configuration file.
# Vi/etc/httpd. conf
Include/etc/httpd/extra/httpd-vhosts.conf
Options FollowSymLinks
AllowOverride none
Require all granted
Virtual Host Configuration
# Vi/etc/httpd/extra/httpd-vhosts.conf
DocumentRoot "/data/www"
ServerName www.linuxidc.com
Create a directory and write it into the index.html file.
Mkdir/data/www
Write index.html in each webdirectory, with the following content: This is LAMP 1 !; This is LAMP 2 !; This is LAMP 3!
# Curl 192.168.20.10
This is LAMP 1!
# Curl 192.168.20.11
This is LAMP 2!
# Curl 192.168.20.12
This is LAMP 3!
Nginx proxy server operation:
Write a proxy configuration file:
# Cat/usr/local/nginx/conf/vhosts/nginxproxy. conf
Upstream backend {
Server 192.168.20.10: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;
Server 192.168.20.11: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;
Server 192.168.20.12: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;
}
Server {
Listen 80;
Server_name www.linuxidc.com;
Index index.html;
Location /{
Proxy_pass https: // backend;
}
}
Add local ip Address Resolution for hosts, simulate the domain name corresponding to the Public ip address, and add resolution for windows local hosts;
# Cat/etc/hosts
192.168.11.30 www.linuxidc.com
Test with curl, rr polling by default, one visit to web1, one web2, one web3
Run the for loop to view the access results:
# For n in 'seq 10'; do curl www.linuxidc.com; sleep 2; done
This is LAMP 2!
This is LAMP 1!
This is LAMP 3!
This is LAMP 2!
This is LAMP 1!
This is LAMP 3!
This is LAMP 2!
This is LAMP 1!
This is LAMP 3!
This is LAMP 2!
2. Configure multiple sites:
In web1, web2, web3, add 2nd sites bbs.linuxidc.com
Add the httpd configuration file to allow access to the website directory;
Options FollowSymLinks
AllowOverride none
Require all granted
Added Virtual Host Configuration
# Vi/etc/httpd/extra/httpd-vhosts.conf
DocumentRoot "/data/bbs"
ServerName bbs.linuxidc.com
Create a directory and write it into the index.html file.
Mkdir/data/bbs
Write index.html in each webdirectory. The content is: This is BBS.linuxidc.com 1 !; This is BBS.linuxidc.com 2 !; This is BBS.linuxidc.com 3!
Nginx server Load balancer and server added to the VM:
Server {
Listen 80;
Server_name bbs.linuxidc.com;
Index index.html;
Location /{
Proxy_pass https: // backend;
Proxy_set_header Host $ host;
Proxy_set_header X-Forwarded-For $ remote_addr;
}
}
Add local ip Address Resolution for hosts, simulate the domain name corresponding to the Public ip address, and add resolution for windows local hosts;
# Cat/etc/hosts
192.168.11.30 www.linuxidc.com bbs.linuxidc.com
Run the for loop to view the access results:
# For n in 'seq 10'; do curl bbs.linuxidc.com; sleep 2; done
This is BBS.linuxidc.com 1!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 1!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 1!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 1!
3. Add ip_hash under upstream;
The test result is as follows: User connection is maintained, which also leads to uneven distribution.
# For n in 'seq 10'; do curl bbs.linuxidc.com; sleep 2; done
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
4. Add an upstream, perform Load Balancing for bbs requests separately, and set weights. The configuration file is as follows:
Upstream backend {
Server 192.168.20.10: 80 weight = 2 max_fails = 3 fail_timeout = 30 s;
Server 192.168.20.11: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;
}
Upstream bbs {
Server 192.168.20.11: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;
Server 192.168.20.12: 80 weight = 2 max_fails = 3 fail_timeout = 30 s;
}
Server {
Listen 80;
Server_name www.linuxidc.com;
Index index.html;
Location /{
Proxy_pass https: // backend;
}
}
Server {
Listen 80;
Server_name bbs.linuxidc.com;
Index index.html;
Location /{
Proxy_pass https: // bbs;
Proxy_set_header Host $ host;
Proxy_set_header X-Forwarded-For $ remote_addr;
}
}
The experiment results are as follows:
# For n in 'seq 10'; do curl bbs.linuxidc.com; sleep 2; done
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 2!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 3!
This is BBS.linuxidc.com 2!
# For n in 'seq 10'; do curl www.linuxidc.com; sleep 2; done
This is LAMP 2!
This is LAMP 1!
This is LAMP 1!
This is LAMP 2!
This is LAMP 1!
This is LAMP 1!
This is LAMP 2!
This is LAMP 1!
This is LAMP 1!
This is LAMP 2!