Nginx implements detailed configuration of Cluster load balancing, nginx Load Balancing
In fact, the Nginx Server Load balancer function is the same as the nginx proxy function. Instead, you only need to change one proxy machine to multiple servers. Compared with lvs, Nginx is a more advanced application layer. It simply forwards user requests to the following machines without any changes to the ip address and kernel. This means that the backend RS does not need to be configured with a public network.
I. experiment environment
Nginx scheduler (public 172.16.254.200 privite 192.168.0.48)
RS1 only has an intranet IP address (192.168.0.18)
RS2 only has an Internet IP address (192.168.0.28)
Ii. Configuration File
Edit the configuration file on the nginx Scheduler
# vim /usr/local/nginx/conf/vhosts/lb.conf
Add the following content:
upstream test { ip_hash; server 192.168.0.18; server 192.168.0.28;}server { listen 80;150 server_name www.aminglinux.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; } }
Iii. configuration instructions
You will find that this configuration is exactly the same as the nginx proxy configuration we mentioned earlier, but an upstream is added. This upstream is used to define the backend RS and can only write one. Ip_hash is a scheduling algorithm of nginx. After this line is added, a user's request is evenly distributed to a fixed RS. This helps avoid session loss when requests from the same user are distributed to different machines. In upstream, you can add weights to the ip address after RS, for example, "server 192.168.31.100 weight = 100 ;". Note that test after upstream is a user-defined name and can be written at will. The only requirement is to be consistent with that after proxy_pass.