load_balancing Server配置文件:
vim /etc/nginx/nginx.conf
server {
listen 80 ;
listen [::]:80 ;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
取消
listen 80 ;
后面的
defalut_server
调度服务器不需要这一项
平均负载(轮询负载)
vim /etc/nginx/conf.d/load_balancion.conf
upstream load_balancing {
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103;
}
server {
listen 80 default_server;
server_name www.yangyang.com;
location / {
proxy_pass http://load_balancing;
}
}
根据权重负载
vim /etc/nginx/conf.d/load_balancion.conf
upstream load_balancing {
server 172.16.69.101 weight=3;
server 172.16.69.102 weight=1;
server 172.16.69.103 weight=2;
}
server {
listen 80 default_server;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
热备
upstream load_balancing {
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103 backup;
}
server {
listen 80 default_server;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
备胎功能,和人一样,只要有选择不选择最坏的,意味着只要 172.16.69.101和 172.16.69.102不是全坏了,不启用 172.16.69.103,哪怕 172.16.69.101坏了,也只是全部调度到 172.16.69.102上,也可以用作Sorry Server,当全部服务器崩盘,它顶上来道歉。
定义最大失败次数
max_fails=最大失败次数
在ngx_http_upstream_module模块里,需要放在upstream的括号路,默认为1次
upstream load_balancing {
max_fails=5;
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103=;
}
server {
listen 80 default_server;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
定义失败之后重发的间隔时间
fail_timeout=时间
在ngx_http_upstream_module模块里,需要放在upstream的括号路,默认为10秒
upstream load_balancing {
max_fails=5;
fail_timeout=10s;
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103;
}
server {
listen 80 default_server;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
down
upstream load_balancing {
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103 down;
}
server {
listen 80 default_server;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
暂时停止使用172.16.69.103服务器,暂时下线,适用于灰度发布。上线的时候再把这个取消即可。
调度算法
ip_hash
根据IP地址来进行分配,可以实现会话保持,但是因为我们是用公网地址来访问的,大量局域网用户会使用同一个IP访问,也会造成负载不均。
upstream load_balancing {
ip_hash;
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103 down;
}
server {
listen 80 default_server;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
least_conn
最小连接数
根据连接数来分配,拥有最少的连接的服务器会接受分配
upstream load_balancing {
least_conn;
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103;
}
server {
listen 80 default_server;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
负载均衡实现不同子域转发
ngx_ http_ proxy_ module — proxy_ pass
访问不同的网站地址,不能显示不同的网站页面
proxy_set header Host $host;
upstream load_balancing {
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103;
}
server {
listen 80 default_server;
proxy_set header Host $host;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
server {
listen 80 default_server;
proxy_set header Host $host;
server_name blog.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
server {
listen 80 default_server;
proxy_set header Host $host;
server_name bbs.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
负载均衡实现日志记录客户端地址
访问网站用户地址信息无法进行分析统计
需要添加一下
proxy_ set_ header X- Forwarded- For $remote addr;
upstream load_balancing {
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103;
}
server {
listen 80 default_server;
proxy_set header Host $host;
proxy_ set_ header X- Forwarded- For $remote addr;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
server {
listen 80 default_server;
proxy_set header Host $host;
proxy_ set_ header X- Forwarded- For $remote addr;
server_name blog.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
server {
listen 80 default_server;
proxy_set header Host $host;
proxy_ set_ header X- Forwarded- For $remote addr;
server_name bbs.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
跳过错误页面
proxy_next_upstream
访问负载均衡会出现错误页面,影响用户体验
proxy_ next_upstream error timeout http_ 404 http_ 502 http_ 403;
还可以加以下的
proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off …;
upstream load_balancing {
server 172.16.69.101;
server 172.16.69.102;
server 172.16.69.103;
}
server {
listen 80 default_server;
proxy_set header Host $host;
proxy_ set_ header X- Forwarded- For $remote addr;
proxy_ next_upstream error timeout http_ 404 http_ 502 http_ 403;
server_name www.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
server {
listen 80 default_server;
proxy_set header Host $host;
proxy_ set_ header X- Forwarded- For $remote addr;
proxy_ next_upstream error timeout http_ 404 http_ 502 http_ 403;
server_name blog.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}
server {
listen 80 default_server;
proxy_set header Host $host;
proxy_ set_ header X- Forwarded- For $remote addr;
proxy_ next_upstream error timeout http_ 404 http_ 502 http_ 403;
server_name bbs.yangyang.host;
location / {
proxy_pass http://load_balancing;
}
}