# nginx 负载均衡配置
- 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
upstream my_server {
server 192.168.0.2:8080;
server 192.168.0.3:8080;
}
server {
listen 80;
server_name 192.168.0.1;
# Path to the root of your installation
location / {
proxy_pass http://my_server;
}
}
- weight 权重策略
weight 代表权重,默认为 1,权重越高被分配的客户端越多,指定轮询几率。weight 和访问比率成正比,用于后端服务器性能不均的情况。
upstream my_server {
server 192.168.0.2:8080 weight=1;
server 192.168.0.3:8080 weight=2;
}
server {
listen 80;
server_name 192.168.0.1;
# Path to the root of your installation
location / {
proxy_pass http://my_server;
}
}
- ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。
upstream my_server {
ip_hash;
server 192.168.0.2:8080;
server 192.168.0.3:8080;
}
server {
listen 80;
server_name 192.168.0.1;
# Path to the root of your installation
location / {
proxy_pass http://my_server;
}
}
- fair (第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream my_server {
server 192.168.0.2:8080;
server 192.168.0.3:8080;
fair;
}
server {
listen 80;
server_name 192.168.0.1;
# Path to the root of your installation
location / {
proxy_pass http://my_server;
}
}
- 动静分离
把静态的资源,比如图片,css,js 等先加载到 Nginx 的服务器里。