Nginx负载均衡和Keepalived的安装设置
一、Nginx设置负载均衡
(1)upstream的配置
http { upstream backend { #这里设置后台分发的服务器族群,有多少个可以添加,同时设置查询策略
server 192.168.3.148:80;
server 192.168.3.148:8080;
} include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on;
#tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;
}
upstream的配置方式:
nginx 的upstream目前支持4种方式的分配
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器 ,如果后端服务器down掉,能自动剔除。
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session 的问题。
例如:
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
细节:
upstream bakend{ #定义负载均衡 设备的Ip及设备状态
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
client_body_in_file_only 设置为On,可以将client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录,可以设置最多3层目录
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
(2)server的配置
server {
listen 8001;
server_name localhost;
set $root_path '/var/www/html'; #charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main; location / {
proxy_set_header host $host;
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header X-real-ip $remote_addr; #获取经过负载均衡转发后实际的客户端IP
proxy_pass http://backend; #这里的proxy_pass设置成http
#root $root_path;
#index index.html index.htm index.php;
} #location /API/public {
# try_files $uri $uri/ /index.php?$query_string;
#} #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;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://backend;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root $root_path;
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
二、Keepalived的设置
(1)安装
sudo apt-get install keepalived
(2)配置
sudo vim /etc/keepalived/keepalived.conf (keepalived.conf不存在,则创建)
Master机:
global_defs {
notification_email {
123@163.com #警告邮箱
}
notification_email_from 123@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_Twioo
}
vrrp_instance VI_1 {
state MASTER ### 设置为主机
interface eth0 ### 监控网卡
virtual_router_id 51 ### 这个两台服务器必须一样
priority 101 ### 权重值 MASTRE 一定要高于 BAUCKUP,有的地方说是至少要比BACKUP高50
authentication {
auth_type PASS ### 加密
auth_pass eric ### 加密的密码,两台服务器一定要一样,
不然会出错
}
virtual_ipaddress {
192.168.0.222 ### VIP 地址
}
}
Backup机(基本和Master一样,有些地方需要更改即可):
global_defs {
notification_email {
123@163.com #警告邮箱
}
notification_email_from 123@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_Twioo
}
vrrp_instance VI_1 {
state BACKUP ### 设置为主机
interface eth0 ### 监控网卡
virtual_router_id 51 ### 这个两台服务器必须一样
priority 11 ### 权重值 MASTRE 一定要高于 BAUCKUP,有的地方说是至少要比BACKUP高50
authentication {
auth_type PASS ### 加密
auth_pass eric ### 加密的密码,两台服务器一定要一样,
不然会出错
}
virtual_ipaddress {
192.168.0.222 ### VIP 地址
}
}
自己可以设置监控脚本
Nginx负载均衡和Keepalived的安装设置的更多相关文章
- 企业级Nginx负载均衡与keepalived高可用实战(二)keepalived篇
1.Keepalived高可用软件 1.1.Keepalived介绍 Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实 ...
- 企业级Nginx负载均衡与keepalived高可用实战(一)Nginx篇
1.集群简介 1.1.什么是集群 简单地说,集群就是指一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行各自服务的独立服务器. ...
- 手把手教你玩转nginx负载均衡(二)----安装虚拟机操作系统
引言 在上一篇,我们组装好了虚拟机的硬件部分,那么现在我们就要把操作系统装上了,既然是服务器,那么安装linux操作系统是个比较好的选择,如果你喜欢的话,安装windows也是没有任何问题的 我这里选 ...
- nginx负载均衡配合keepalived服务案例实战
本实验用4台 centos6 虚拟机,2台做负载均衡,2台做web服务器,都先装上nginx lb01:192.168.0.235 --主负载均衡器 lb02:192.168.0.236 --备负 ...
- Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用
上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常运行.针对系统架构设计的高可用要求,我们需要解决Nginx ...
- Keepalived+LVS+Nginx负载均衡之高可用
Keepalived+LVS+Nginx负载均衡之高可用 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常 ...
- 4、keepalived高可用nginx负载均衡
keepalived: HTTP_GET //使用keepalived获取后端real server健康状态检测 SSL_GET(https) //这里以为这后端使用的是http协议 ...
- Nginx(七):keepalived实现Nginx负载均衡服务器的双机高可用
前言 之前咱们通过 Nginx(六):Nginx HTTP负载均衡和反向代理的配置与优化 和 Nginx+tomcat组合实现高并发场景的动静分离和负载均衡方案 这两篇文章了解了Nginx对高并发应用 ...
- JAVAEE——宜立方商城03:Nginx负载均衡高可用、Keepalived+Nginx实现主备
1 nginx负载均衡高可用 1.1 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务, ...
随机推荐
- drupal错误: Maximum execution time of 240 seconds exceeded
drupal7.5安装完成,导入汉化包时,出现错误: Fatal error: Maximum execution time of 240 seconds exceeded in D:\phpweb\ ...
- OpenGL红宝书第一个例子:绘制两个三角形
1. 环境配置 在这里不在做环境配置的说明,因为网上可以找到很多类似的教程,如果有需要可以@我,我也希望能帮到大家,其它的不说了,先上我的代码 2. 第一个程序代码 创建LoadShader.h #p ...
- POJ 3368/RMQ/线段数
题目链接 /* 给出一段序列,询问[L,R]区间内最大相同数的个数. 用一个很巧妙地方法,转化成求区间内的最大值的问题. RMQ维护区间最大值. MAX处理: */ for(int i=1;i< ...
- 2015年4月29日 dayofweek
#include <stdio.h>#include <stdlib.h>int DayofYear(int year, int month, int day);#define ...
- math ceil以及math floor,math:round
◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数:◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数:◎Math.round()执行标准舍入,即它总 ...
- HDU ACM 1495 非常可乐(广搜BFS)
非常可乐 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- Nape "不动"刚体的碰撞交互
"不动"刚体,这里指的是以下两类刚体: 1.刚体的allowMovement和allowRotation都设false的刚体 2.KINEMATIC类型的刚体 如下代码,测试发现输 ...
- 英文SEO外部链接资源收集之常用的footprints
inurl:/privacy-policy "Using Article Directory plugin"inurl:/terms "Using Article D ...
- ubuntu 解压 windows 生成的 zip 文件乱码问题
在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 有两种方式解决问题:(建议采用方法 ...
- 强制性输出private中变量的三种方法
众所周知,private里面的变量不可以输出,但是也可以通过特殊途径获得. 1.通过指针暴力内存,把它索罗出来,方法:调试,破掉语法. 并且还可以对类对象进行修改. // Thread.cpp : 定 ...