Abstract: Load balancing: Load balancing is a technology used to distribute incoming network traffic among multiple servers to balance the load on the server and improve system availability and performance. When you have multiple servers, you can use load balancing to distribute requests across those servers to prevent a single server from being overloaded and impacting the user experience.
Reverse proxy: A reverse proxy is a server configuration that forwards incoming client requests to a backend server and returns responses from the backend server to the client. In a load-balanced environment, a reverse proxy sits between the client and the backend server. The client sends the request to the reverse proxy, which then forwards the request to one or more backend servers. Reverse proxies can also perform other tasks such as SSL termination, security enhancement, caching, etc.
Summary: When configuring Nginx for load balancing, you will configure a reverse proxy server that will be responsible for distributing incoming requests to the backend servers. This configuration can improve the scalability and stability of the system. Typically, you would use Nginx's upstream directive to define a list of backend servers and use location blocks to forward requests to those backend servers.
1. Install nginx (required for all three hosts)
上传rpm包,使用yum安装
[root@node1 ~]# yum install nginx-1.22.0-1.el7.ngx.x86_64.rpm -y
2. Configure two test web servers (both web servers node3 and node4 are executed)
[root@node3 ~]# cd /etc/nginx/conf.d/
[root@node3 conf.d]# mv default.conf{,.bak}
[root@node3 conf.d]# vim vhost.conf
# 配置虚拟主机
server {
listen 80;
server_name bbb.itxuan.com;
location / {
root /usr/share/nginx/html/bbb;
index index.html index.htm;
}
access_log /usr/share/nginx/html/bbb/logs/access_bbb.log main;
}
server {
listen 80;
server_name www.itxuan.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
access_log /usr/share/nginx/html/www/logs/access_www.log main;
}
3. Configure the web test page (execute both node3 and node4)
[root@node3 conf.d]# mkdir -p /usr/share/nginx/html/{www,bbb}/logs
[root@node3 conf.d]# echo "`hostname -I ` www" > /usr/share/nginx/html/www/index.html
[root@node3 conf.d]# echo "`hostname -I ` bbb" > /usr/share/nginx/html/bbb/index.html
4. Start the service for testing
[root@node1 ~]# systemctl start nginx
[root@node1 ~]# curl -H host:bbb.itxun.com 192.168.136.164
192.168.136.164 bbb
[root@node1 ~]# curl -H host:bbb.itxun.com 192.168.136.163
192.168.136.163 bbb
5. Configure load balancing on node1
[root@node1 ~]# vim /etc/nginx/conf.d/default.conf
upstream www_server_pools{
server 192.168.136.163:80;
server 192.168.136.164:80;
}
server {
listen 80;
server_name www.itxuan.com;
#access_log /var/log/nginx/host.access.log main;
location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
proxy_pass http://www_server_pools;
}
nginx checks and restarts the service
[root@node1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1 ~]# systemctl restart nginx
6. Configure node1 hosts resolution and test
[root@node1 ~]# vim /etc/hosts
192.168.136.161 www.itxuan.com
[root@node1 ~]# for ((i=1;i<=4;i++)); do curl http://www.itxuan.com; done
192.168.136.164 bbb
192.168.136.163 bbb
192.168.136.164 bbb
192.168.136.163 bbb
7. Stop any node for testing.
[root@node3 ~]# systemctl stop nginx
[root@node1 ~]# for ((i=1;i<=4;i++)); do curl http://www.itxuan.com; done
192.168.136.164 bbb
192.168.136.164 bbb
192.168.136.164 bbb
192.168.136.164 bbb
7. Test after the node returns to normal
[root@node3 ~]# systemctl start nginx
[root@node1 ~]# for ((i=1;i<=4;i++)); do curl http://www.itxuan.com; done
192.168.136.164 bbb
192.168.136.164 bbb
192.168.136.163 bbb
192.168.136.164 bbb