栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx配置负载均衡

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx配置负载均衡

nginx配置文件路径:

apt安装的nginx配置文件路径:/etc/nginx/nginx.conf

Nginx有6中配置负载均衡的方法:
  1. 轮询算法(默认)
    请求被平均调度给多个服务器处理
http {
  ...
  upstream myserver {
   server 192.168.1.2:8080;
   server 192.168.1.3:8080;
   ...
  }
  server {
  	  ...
	  location / {
	  	proxy_pass http://myserver;
	  }
  }
}
  1. 加权轮询算法
    给要轮询的服务器配置权重
http {
	...
	upstream myserver {
	  server 192.168.1.2:8080 weight=10;
	  server 192.168.1.3:8080 weight=90;
	  ...
	}
	...
	server {
		...
		localtion / {
		  proxy_pass http:myserver;
		}
	}
}
  1. ip_hash算法
    将客户端ip进行hash运算,然后将请求转发到对应的服务器,每台客户端对应的服务器是固定的
http {
	...
	upstream myserver {
		ip_hash;
		server 192.168.1.2:8080;
		server 192.168.1.3:8080;
		...
	}
	...
	server {
	    ...
		location / {
		  proxy_pass http://myserver;
		}
	}
}
  1. 最少连接数算法
    此种方式nginx会将请求转发到当前连接数较少的服务器
http {
	...
	upstream myserver {
	  least_conn;
	  server 192.168.1.2:8080;
	  server 192.168.1.3:8080;
	  ...
	}
	...
	server {
	  ...
	  location / {
	    proxy_pass http://myserver;
	  }
	}
}
  1. url_hash算法
    将客户端url进行hash运算,然后将请求转发到相应的服务器,这种方式相同url会转发到同一个服务器
    需要安装url_hash补丁
http {
  ...
  upstream myserver {
    server 192.168.1.2:8080;
    server 192.168.1.3:8080;
    ...
    hash $request_uri;
    hash_method crc32;
  }
  ...
  server {
    ...
    location / {
      proxy_pass http://myserver;
    }
  }
}
  1. fair算法
    优先选择响应时间最短的服务器
    需要安装模块:nginx-upstream-fair-master
http {
  ...
  upstream myserver {
    fair;
    server 192.168.1.2:8080;
    server 192.168.1.3:8080;
    ...
  }
  ...
  server {
    ...
    location / {
      proxy_pass http://myserver;
    }
  }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/827626.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号