Nginx 负载均衡配置
Nginx 负载均衡配置
负载均衡分类
- 一种是通过硬件来进行解决,常见的硬件有 NetScaler、F5、Radware 和 Array 等商用的负载均衡器,但是它们是比较昂贵的
- 一种是通过软件来进行解决的,常见的软件有 LVS、Nginx、apache 等,它们是基于 Linux系统并且开源的负载均衡策略.
nginx实现方式
- 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。 - weight权重 ——you can you up
指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。 - ip_hash ip 哈希算法
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。
配置示例(conf文件)
- www.aaa.com
upstream bakeaaa { ip_hash; server 127.0.0.1:3001 ; } server { listen 80; server_name www.aaa.com; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffering off; proxy_pass http://bakeaaa; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
- www.bbb.com
upstream bakebbb { ip_hash; server 127.0.0.1:3002 ; } server { listen 80; server_name www.bbb.com; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffering off; proxy_pass http://bakebbb; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
- www.ccc.com
upstream bakeccc { ip_hash; server 192.168.1.129:3001; } server { listen 80; server_name www.ccc.com; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_buffering off; proxy_pass http://bakeccc; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
相关推荐
swtIrene 2020-08-14
slovyz 2020-08-14
tinydu 2020-08-03
yungame 2020-07-04
廖先贵 2020-06-23
畅聊架构 2020-06-10
魏莉的微 2020-06-07
后厂村老司机 2020-06-01
后厂村老司机 2020-05-08
azhuye 2020-11-12
liupengqwert 2020-10-28
YzhilongY 2020-08-31
crazyjingling 2020-08-16
tinydu 2020-08-09
Studynutlee 2020-08-03
快乐de馒头 2020-07-29
yungame 2020-07-27
buaichidoufu 2020-07-28