nginx 负载均衡

三台装有nginx的虚拟机,一台做反向代理服务器,另外两台做真实服务器,模拟负载均衡。

192.168.13.129       #反向代理服务器
192.168.13.133       #真实服务器
192.168.13.139       #真实服务器


三台服务器的配置

我们在反向代理服务器上(192.168.13.139)进行如下配置:
[root@real-server ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下内容
upstream test_server {
      server 192.168.13.133:8080;
      server 192.168.13.139:8080;
    }
 server {
        listen 80;
        server_name localhost;
        location / {
           proxy_pass  http://test_server;
        }
}
[root@server ~]# nginx -t
[root@server ~]# nginx -s reload
第一台真实服务器配置(192.168.13.133):
[root@real-server ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下内容
server {
        listen 80;
        server_name localhost;
        location / {
                root    /usr/share/nginx/html/login;
                index   index.html index.html;
                }
}
[root@real-server ~]# nginx -t
[root@real-server ~]# nginx -s reload
#创建目录和文件并写入测试数据
[root@real-server ~]# mkdir -p /usr/share/nginx/html/login
[root@real-server ~]# echo "this is first real-server" > /usr/share/nginx/html/login/index.html
第二台真实服务器配置(192.168.13.139):
[root@real-server ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下内容
server {
        listen 80;
        server_name localhost;
        location / {
                root    /usr/share/nginx/html/login;
                index   index.html index.html;
                }
}
[root@real-server ~]# nginx -t
[root@real-server ~]# nginx -s reload
#创建目录和文件并写入测试数据
[root@real-server ~]# mkdir -p /usr/share/nginx/html/login
[root@real-server ~]# echo "this is second real-server" > /usr/share/nginx/html/login/index.html

测试:打开网页输入反向代理服务器ip:http://192.168.13.129/

原文地址:https://www.cnblogs.com/plyc/p/14253430.html