nginx负载均衡
一、nginx安装
详见:http://blog.csdn.net/zhengyong15984285623/article/details/53871752
二、负载均衡机制
least_conn、ip_hash;
在nginx中支持以下负载均衡机制:
- 轮询 - 向应用服务器的请求以循环方式分布,
- 最小连接 - 下一个请求分配给具有最少活动连接数的服务器,
- ip-hash - 散列函数用于确定应为下一个请求选择什么服务器(基于客户端的IP地址)。
三、负载均衡示例
轮询方式(默认):
http {
upstream myapp1 {
server srv1.example.com max_fails=3 fail_timeout=30s;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://myapp1;
}
}
}
最小连接:
upstream myapp1 {
least_conn
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
hash方式:
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
权重方式:
upstream myapp1 {
server srv1.example.com weight=3;
server srv2.example.com;
server srv3.example.com;
}
四、参考文档
参考文档地址:http://nginx.org/en/docs/http/load_balancing.html#nginx_load_balancing_configuration