轻量级服务器Nginx
1. Nginx介绍
- Nginx(engine x)是一款由俄罗斯的程序设计师Lgor Sysoev所开发高性能的web和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
- 轻量级的web服务器
2. Nginx的准备
1)下载命令:wget http://nginx.org/download/nginx-1.15.5.tar.gz -P /usr/src
2)配置:
(1)检查环境 是否 满足安装条件 依赖解决
(2)指定安装方式 配置文件 命令文件 各种文件放哪里 开启模块功能
(3)指定软件安装在那里
3)安装前准备
cd /usr/src
tar xf nginx-1.15.5.tar.gz
cd nginx-1.15.5
yum -y install gcc pcre-devel zlib zlib-devel
4)编译 使用gcc将源码生成可执行程序:make -j4
5)安装:make install
6)Nginx的启动:/usr/local/nginx/sbin/nginx
7)验证端口是否在被使用: netstat –ntpl lsof -i :80
3. Nginx配置文件详解
/usr/local/nginx/conf/nginx.conf
#启动子进程程序的默认用户
#user nobody;
# 一个主进程和多个工作进程。工作进程是单进程的,且不需要特殊授权即可运行;这里定义的是工作进程数量
worker_processes 1;
# 全局错误日志的位置及日志格式
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# 每个工作进程最大的并发数
worker_connections 1024;
}
# http服务器设置
http {
# 设定mime类型,类型由mime.type文件定义
include 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"';
# $remote_addr与$http_x_forwarded_for用以记录客户端的ip地址
# $remote_user用以记录客户端用户名称
# $time_local用以记录访问时间与时区
# $request用以记录请求的url与http协议
# $status用来记录请求状态;成功是200,
# $body_bytes_sent记录发送给客户端文件主体内容大小
# $http_referer用来记录从哪个页面链接访问过来的
# $http_user_agent记录客户浏览器的相关信息
# 全局访问日志路径
#access_log logs/access.log main;
# sendfile指令指定nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设为on
sendfile on;
# 此选项允许或禁止使用socket的TCP_CORK的选项,此选项尽在使用sendfile的时候使用
#tcp_nopush on;
# 长连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
# 开启压缩
#gzip on;
# 配置虚拟主机
server {
# 虚拟主机使用的端口
listen 80;
# 虚拟主机域名
server_name localhost;
# 虚拟主机支持的字符集
#charset koi8-r;
# 虚拟主机的访问日志路径
#access_log logs/host.access.log main;
# 定义web根路径
location / {
# 根目录路径
root html;
# 索引页
index index.html index.htm;
}
#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 html;
}
# 定义反向代理服务器 数据服务器是lamp模型
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 定义PHP为本机服务的模型
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
4. Nginx默认网站
当Nginx配置⽂文件中有且只有⼀一个Server的时候,该Server就被Nginx认为是默认⽹网站,所有发给Nginx服务器器80端⼝口的数据都会默认给该Server.
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
1)Nginx目录访问权限
location /a {
allow 192.168.1.0/24;
deny all;
#return 404;
return http://www.baidu.com;
}
2)登陆验证
• auth_basic
语法: auth_basic string | off;
默认值: auth_basic off; • auth_basic_user_file file;
location /c {
auth_basic "登陆验证";
auth_basic_user_file /etc/nginx/htpasswd;
}
3)日志格式
Nginx访问日志主要有两个参数控制
1) log_format #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
log_format log_name string
log_format格式变量:
- $remote_addr #记录访问网站的客户端地址
- $remote_user #远程客户端用户名
- $time_local #记录访问时间与时区
- $request #用户的http请求起始行行信息
- $status #http状态码,记录请求返回的状态码,例如:200、301、404等
- $body_bytes_sent #服务器发送给客户端的响应body字节数
- $http_referer #记录此次请求是从哪个连接访问过来的,可以根据该参数进⾏行防盗链设置。
- $http_user_agent #记录客户端访问信息,例如:浏览器、⼿手机客户端等
- $http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器器也要进行行相关的x_forwarded_for设置
2) access_log #用来指定日志文件的路径及使用的何种日志格式记录日志
access_log logs/access.log main;
自定义日志格式为json
log_format main_json '{"@timestamp":"$time_local",'
'"client_ip": "$remote_addr",'
'"request": "$request",'
'"status": "$status",'
'"bytes": "$body_bytes_sent",'
'"x_forwarded": "$http_x_forwarded_for",'
'"referer": "$http_referer"'
'}’;
access_log logs/access_json.log main_json;
3)防盗链
location /images/ {
alias /data/images/;
valid_referers none blocked *.ayitula.com;
if ($invalid_referer) {
return 403;
}
}
4)日志截断
mv access.log access.log.0
killall -USR1 `cat master.nginx.pid`
sleep 1
gzip access.log.0
5. Nginx虚拟主机
一个web服务器软件默认情况下只能发布一个web,因为一个web分享出去需要三个条件(IP、Port、Domain name)
一个web服务器软件如何发布多个web呢?
虚拟主机:就是把一台物理理服务器划分成多个“虚拟”的服务器,每一个虚拟主机都可以有独立的域名和独立的目录
1)基于IP的虚拟主机!
server {
listen 192.168.10.42:80;
location / {
root html/abc;
index index.html index.htm index.php; } }
server {
listen 192.168.10.52:80;
location / {
root html/cbd;
index index.html index.htm; } }
2)基于端⼝的虚拟主机
server {
listen 80;
server_name www.abc.com;
location / {
root html/abc;
index index.html index.htm index.php;
}
}
server {
listen 8080;
server_name www.abc.com;
location / {
root html/cbd;
index index.html index.htm;
}
}
6. 基于域名的虚拟主机
server {
listen 80;
server_name www.abc.com;
location / {
root html/abc;
index index.html index.htm index.php;
}
}
server {
listen 80;
server_name www.cbd.com;
location / {
root html/cbd;
index index.html index.htm;
}
}
7. 反向代理
1)反向代理介绍
代理服务器,客户机在发送请求时,不会直接发送给⽬的主机,而是先发送给代理服务器,代理服务接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的硬盘中,再发送给客户机。
2)应用场景
• 堡垒机场景
• 内⽹网服务器器发布场景
• 缓存场景
3)反向代理原理
(1)客户端通过浏览器器 发起请求 代理理服务器器
(2)代理理服务器器 接受请求
(3)代理理服务器器 发起请求 业务服务器器
(4)业务服务器器 接受请求
(5)业务服务器器 处理理请求
(6)业务服务器器 响应请求 代理理服务器器
(7)代理理服务器器 响应请求 客户端
(8)客户端通过浏览器器渲染请求并展示给⽤用户
4)反向代理实现
location / {
index index.php index.html index.htm; #定义首页索引文件的名称
proxy_pass http://mysvr ;#请求转向mysvr 定义的服务器列列表
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器器传
}
8. 限速
限速该特性可以限制某个用户在一个给定时间段内能够产生的HTTP请求数。请求可以简单到就是一个对于主页的GET请求或者一个登陆表格的POST请求。
限速也可以用于安全目的上,比如暴力密码破解攻击。通过限制进来的请求速率,并且(结合日志)标记出目标URLs来帮助防范DDoS攻击。一般地说,限流是用在保护上游应用服务器不被在同一时刻的大量用户请求湮没。
1)应用场景
• DDOS防御
• 下载场景保护IO
2)限速原理
算法思想是:
水(请求)从上方倒入水桶,从水桶下方流出(被处理);
来不及流出的水存在水桶中(缓冲),以固定速率流出;
水桶满后水溢出(丢弃)。
这个算法的核心是:缓存请求、匀速处理、多余的请求直接丢弃。
3)限速实现
Nginx官方版本限制IP的连接和并发分别有两个模块:
limit_req_zone 用来限制单位时间内的请求数,即速率限制。
limit_req_conn 用来限制同一时间连接数,即并发限制。
3)模块使用⽅方法
limit_req_zone 参数配置
Syntax: limit_req zone=name [burst=number] [nodelay];
Default: —
Context: http, server, location
4)限速案例⼀
#基于IP对下载速率做限制 限制每秒处理1次请求,对突发超过5个以后的请求放入缓存区
http {
limit_req_zone $binary_remote_addr zone=baism:10m rate=1r/s;
server {
location /search/ {
limit_req zone=baism burst=5 nodelay;
}
}
limit_req_zone $binary_remote_addr zone=baism:10m rate=1r/s;
第一个参数:$binary_remote_addr 表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址。
第二个参数:zone=baism:10m表示生成一个大小为10M,名字为one的内存区域,用来存储访问的频次信息。
第三个参数:rate=1r/s表示允许相同标识的客户端的访问频次,这里限制的是每秒1次,还可以有比如30r/m的。
limit_req zone=baism burst=5 nodelay;
第一个参数:zone=baism 设置使用哪个配置区域来做限制,与上面limit_req_zone里的name对应。
第二个参数:burst=5,重点说明一下这个配置,burst爆发的意思,这个配置的意思是设置一大小为5的缓冲区当有大量请求(爆发)过来时,超过了访问频次限制的请求可以先放到这个缓冲区内。
第三个参数:nodelay,如果设置,超过访问频次而且缓冲区也满了的时候就会直接返回503,如果没有设置,则所有请求会等待排队。
5)限速案例二
#基于IP做连接限制 限制同⼀一IP并发为1 下载速度为100K
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /abc {
limit_conn addr 1;
limit_rate 100k;
}
}
6)综合案例
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#基于IP做连接限制 限制同一IP并发为1 下载速度为100K
limit_conn_zone $binary_remote_addr zone=addr:10m;
#基于IP对下载速率做限制 限制每秒处理理1次请求,对突发超过5个以后的请求放⼊入缓存区
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /abc {
limit_req zone=one burst=5 nodelay;
limit_conn addr 1;
limit_rate 100k;
}
}
9. URL重写
1)URL rewrite 介绍
• rewrite模块(ngx_http_rewrite_module)
• Rewrite功能是Nginx服务器提供的一个重要功能。几乎是所有的web产品必备技能,用于实现URL重写。URL重写是非常有用的功能,比如它可以在我们在改变网站结构后,不需要客户端修改原来的书签,也不需要其他网站修改对我们网站的友情链接,还可以在一定程度上提高网站的安全性,能够让我们的网站显得更专业。
• Nginx服务器Rewrite功能的实现是依赖于PCRE(Perl Compatible Regular Expression。Perl兼容的正则表达式)的支持,所以在编译安装Nginx之前, 需要安装PCRE库
2)应用场景
• 域名变更
• 用户跳转 (从某个连接跳到另一个连接)
• 伪静态场景 (便于CDN缓存动态页面数据)
3)URL 模块语法
(1) set 设置变量
(2) if 负责语句中的判断
(3) return 返回返回值或URL
(4) break 终止后续的rewrite规则
(5) rewrite 重定向URL
4)Rewrite规则相关指令
(1)set指令 自定义变量
Syntax: set $variable value;
Default: —
Context: server, location, if
案例:将http://www.xxxx.com 重写为 http://www.xxxx.com/baiu
location / {
set $name baiu;
rewrite ^(.*)$ http://www.ayitula.com/$name; }
}
(2)if 指令 负责判断
Syntax: if (condition) { ... }
Default: —
Context: server, location
案例:
location / {
root html;
index index.html index.htm;
if ($http_user_agent ~* 'Chrome') {
return 403;
#return http://www.jd.com;
}
}
#模糊匹配 ~匹配 !~不匹配 ~* 不区分大小写的匹配
#精确匹配 = !=
(3)return 指令 定义返回数据
Syntax: return code [text]; return code URL; return URL;
Default: —
Context: server, location, if
案例:
location / {
root html;
index index.html index.htm;
if ($http_user_agent ~* 'Chrome') {
return 403;
#return http://www.jd.com;
}
}
(4)break 指令 停⽌执行行当前虚拟主机的后续rewrite指令集
Syntax: break;
Default:—
Context:server, location, if
案例:
location / {
root html;
index index.html index.htm;
if ($http_user_agent ~* 'Chrome') {
break;
return 403;
}
}
(5)rewrite 语法
rewrite <regex> <replacement> [flag];
关键字 正则 替代内容 flag标记
flag:
last #本条规则匹配完成后,继续向下匹配新的location URI规则
break #本条规则匹配完成即终⽌,不再匹配后⾯面的任何规则
redirect #返回302临时重定向,浏览器器地址会显示跳转后的URL地址
permanent #返回301永久重定向,浏览器器地址栏会显示跳转后的URL地址
域名跳转案例 www.xxx.com 重写为 www.jd.com
server {
listen 80;
server_name www.xxx.com;
location / {
rewrite ^/$ http://www.jd.com permanent ;
}
}
注意: 重定向就是将网页自动转向重定向
301永久性重定向:新网址完全继承旧网址,旧网址的排名等完全清零 301重定向是网页更改地址后对搜索引擎友好的好方法,只要不是暂时搬移的情况,都建议使用301来做转址。
302临时性重定向:对旧网址没有影响,但新网址不会有排名
搜索引擎会抓取新的内容而保留旧的网址
break
本条规则匹配完成即终止,不再匹配后面的任何规则
类似临时重定向,返回客户端302
根据用户浏览器重写访问目录
如果是chrome浏览器 就将http://192.168.10.42/$URI 重写为http://192.168.10.42/chrome/$URI
#^ 以什么开头 ^a
#$ 以什么结尾 c$
#. 除了回车以外的任意一个字符
#* 前面的字符可以出现多次或者不出现
#更多内容看正则表达式 re
10. 优化Nginx
标准情况下,软件默认的参数都是对安装软件的硬件标准来设置的,目前我们服务器的硬件资源远远大于要求的标准,所以为了了让服务器性能更加出众,充分利用服务器的硬件资源,我们一般需要优化APP的并发数来提升服务器的性能。
1)优化方案
Nginx是主进程+工作进程模型
worker_processes 1; 工作进程数量按CPU的总核心调整
worker_cpu_affinity 0010 0100 1000; CPU的亲和力
worker_connections 1024; 一个工作进程的并发数
2)长连接
http协议属于TCP协议
优化目标:减少三次握手和四次断开的次数
keepalive_timeout 5; 长连接时间
keepalive_requests 8192; 每个长连接接受最大请求数
3)数据压缩
gzip on; (启⽤gzip 压缩功能)
gzip_proxied any; (nginx 做前端代理理时启⽤该选项,表示无论后端服务器的headers头返回什么信息,都无条件启用压缩)
gzip_min_length 1024; (最⼩压缩的页面,如果页面过于小,可能会越压越大,这里规定大于1K的页面才启用压缩)
gzip_buffers 4 8k; (设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流按照原始数据大小以8K为单位申请4倍内存空间)
gzip_comp_level 3; (压缩级别,1压缩比最小处理速度最快,9压缩比最大但处理最慢,同时也最消耗CPU,一般设置为3就可以了)
gzip_types text/plain text/css application/x-javascript application/javascript application/xml; (么类型的页面或文档启用压缩)
4)客户端缓存
语法: expires [time|epoch|max|off]
默认值: expires off
作用域: http, server, location
location ~.*\.(js|css)?$
{
expires 1h;
}
11. 集群
将多个物理机器组成一个逻辑计算机,实现负载均衡和容错
1)组成要素 (1)VIP: 一个虚IP地址 (2)分发器: nginx (3)数据服务器: Web服务器
2)Nginx集群原理:
在该集群中Nginx扮演的⻆⾊是: 分发器
任务:接受请求、分发请求、响应请求
功能模块:
(1)ngx_http_upstream_module 基于应⽤层分发模块
(2)ngx_stream_core_module 基于传输层分发模块 (1.9开始提供)
Nginx集群其实是:虚拟主机+反向代理+upstream分发模块组成的
虚拟主机:接受和响应请求
反向代理: 带用户去数据服务器拿数据
upstream: 告诉Nginx去哪个数据服务器拿数据
3)数据走向
(1)虚拟主机接受用户请求
(2)虚拟主机去找反向代理
(3)反向代理让去找upstream
(4)upstream 告诉一个数据服务器IP
(5)Nginx去找数据服务器并发起用户的请求
(6)数据服务器接受请求并处理请求
(7)数据服务器响应请求给Nginx
4)配置分发器
Syntax: upstream name { ... }
Default: —
Context: http
#upstream 模块
upstream web {
server 192.168.10.42;
server 192.168.10.43;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
12. Nginx分发算法
1)Nginx集群默认算法
upstream module nginx的upstream⽬前支持4种方式的分配
(1)轮询(默认) 每个请求按时间顺序逐⼀分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
(2)weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
(3)ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
(4)fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。
(5)url_hash(第三方) 按访问url的hash结果来分配请求,使每个url定向到同⼀一个后端服务器器,后端服务器器为缓存时⽐比较有效。
2)Nginx业务服务器状态
(1)down 表示单前的server暂时不参与负载
(2)weight 默认为1,weight越大,负载的权重就越大。
(3)max_fails :允许请求失败的次数默认为1,当超过最大次数时,返回proxy_next_upstream 模块定义的错误
(4)fail_timeout: 失败超时时间,在连接Server时,如果在超时时间之内超过max_fails指定的失败次数,会认为在fail_timeout时间内Server不可用。默认为10s。
(5)backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压⼒会最轻。
测试1:轮询分发
upstream web {
server 192.168.10.42;
server 192.168.10.43;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
}
}
测试2:基于权重的分发
upstream web {
server 192.168.10.42 weight=1;
server 192.168.10.43 weight=2;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
}
}
测试3:ip_hash
upstream web {
ip_hash;
server 192.168.10.42;
server 192.168.10.43;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
}
}
ip_hash算法能够保证来自同样源地址的请求,都分发到同一台主机
3)基于请求头的分发
测试1:基于host分发
http {
upstream web1 {
server 192.168.10.42;
}
upstream web2 {
server 192.168.10.43;
}
server {
listen 80;
server_name www.web1.com;
location / {
proxy_pass http://web1;
}
}
server {
listen 80;
server_name www.web2.com;
location / {
proxy_pass http://web2;
}
}
}
测试2:基于开发语言分发(适用于混合开发的环境)
http {
upstream php {
server 192.168.10.42;
}
upstream html {
server 192.168.10.43;
}
server {
location ~* \.php$ {
proxy_pass http://php;
}
}
location ~* \.html$ {
proxy_pass http://html;
}
}
测试3:基于浏览器分发
upstream elinks { server 192.168.10.42; }
upstream chrome { server 192.168.10.43; }
upstream any { server 192.168.10.42:81; }
server {
listen 80;
server_name www.web1.com;
location / {
proxy_pass http://any;
if ( $http_user_agent ~* Elinks ) {
proxy_pass http://elinks;
}
if ( $http_user_agent ~* chrome ) {
proxy_pass http://chrome;
}
}
}
测试4:基于源ip分发
upstream bj.server {
server 192.168.10.42;
}
upstream sh.server {
server 192.168.10.43;
}
upstream default.server {
server 192.168.10.42:81;
}
geo $geo {
default default;
192.168.10.241/32 bj;
192.168.10.242/32 sh;
}
location / {
proxy_pass http://$geo.server$request_uri;
}
13. 构建Nginx高可用集群
1)keepalvied
Keepalived的作⽤是检测服务器的状态,如果有⼀台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
2)Keepalived获得
wget http://www.keepalived.org/software/keepalived-2.0.8.tar.gz
3)Keepalived 安装
#cat keepalived_install.sh
#!/bin/bash
pkg=keepalived-2.0.8.tar.gz
tar xf $pkg
yum -y install kernel-devel
ln -s /usr/src/kernels/3.10.0-862.14.4.el7.x86_64/ /usr/src/linux
cd keepalived-2.0.8/
yum install openssl-* -y
./configure --prefix=/usr/local/keepalived
make
make install
mkdir -pv /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
4)构建高可用Nginx集群
(1)配置Nginx集群
upstream web {
server 192.168.10.42 max_fails=2 fail_timeout=3;
server 192.168.10.43 max_fails=2 fail_timeout=3;
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
}
}
(2)配置keepalived
#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id NGINX_DEVEL
}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_pid.sh"
interval 2
fall 1
}
vrrp_instance nginx {
state MASTER
interface ens33
mcast_src_ip 192.168.10.40
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.10.213/24
}
}
(3)关联脚本 nginx_pid.sh
#chmod 755 /etc/keepalived/nginx_pid.sh
#cat /etc/keepalived/nginx_pid.sh
#!/bin/bash
nginx_kp_check () {
nginxpid=`ps -C nginx --no-header |wc -l`
if [ $nginxpid -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 1
nginxpid=`ps -C nginx --no-header |wc -l`
if [ $nginxpid -eq 0 ];then
systemctl stop keepalived
fi
fi
}
(4)启动keepalived
systemctl start keepalived
(5)配置备份Nginx分发器
backuo.ayitula.com
#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id NGINX_DEVEL
}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_pid.sh"
interval 2
fall 1
}
vrrp_instance nginx {
state BACKUP
interface ens33
mcast_src_ip 192.168.10.41
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.10.213/24
}
}
14.
15.
16.