Linux安装Nginx使用负载均衡

1.实验准备
准备三台计算机
nginx1 192.168.13.121 作为nginx负载均衡器
nginx2 192.168.13.24  web服务,提供一个页面        
nginx3 192.168.13.79  web服务,提供一个页面


2.先配置两个nginx  web页面  
192.168.13.24 准备一个 index.html
192.168.13.79 准备一个 index.html
然后启动两个nginx web 服务
    
    
3.准备一个nginx负载均衡器192.168.13.121机器上,修改nginx.conf
写入如下内容
定义一个负载均衡池,负载均衡的算法有
调度算法      概述
轮询        按时间顺序逐一分配到不同的后端服务器(默认)
weight       加权轮询,weight值越大,分配到的访问几率越高
ip_hash      每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash      按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn    最少链接数,那个机器链接数少就分发
1.轮询(不做配置,默认轮询)
2.weight权重(优先级)
3.ip_hash配置,根据客户端ip哈希分配,不能和weight一起用

upstream s15webserver  {
ip_hash;
server 192.168.13.79 ;
server 192.168.13.24 ;
}
 
然后在虚拟主机中添加 反向代理配置,将用户的请求,直接转发给 负载均衡池中的服务器

server {
        listen       80;
        #当我的请求来自于 192.168.13.121时,走这>个虚拟主机
        server_name  192.168.13.121;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #核心配置,就在这,一条proxy_psss参数即可
        location / {
          proxy_pass http://s15webserver;
            #root   html;
            #index  index.html index.htm;
        }

}


4.启动负载均衡器的 nginx服务

5.在客户端windows中测试访问,负载均衡器  192.168.13.121 ,查看请求分发的结果

原文地址:https://www.cnblogs.com/apollo1616/p/10223642.html