Nginx+keepalive

说明:
2台web服务器,每台有3个web页面。 2台LB负载均衡Nginx
web01 10.14.21.81 lb01 10.14.21.86
web02 10.14.21.82 lb02 10.14.21.87

目录
一、配置web01三个web静态页面www.81.com、bbs.81.com、ftp.81.com
二、配置web02三个web静态页面www.81.com、bbs.81.com、ftp.81.com
三、lb01上测试两个web服务
四、配置lb01反向代理(增加upstream和proxy_pass两个模块)
五、配置keepalive高可用
六、案例:根据淘宝网站URI信息抛送给相应服务器(重点)
七、案例:根据不同设备抛送不同web页面

一、配置web01

[root@web01 ~]# yum install -y pcre-devel openssl-devel    #安装依赖包
[root@web01 ~]# useradd www -s /sbin/nologin/ -M       #创建管理nginx虚拟用户
[root@web01 ~]# cd /opt
[root@web01 opt]# tar zxf nginx-1.16.1.tar.gz             #解压二进制文件
[root@web01 opt]# cd nginx-1.16.1
[root@web01 nginx-1.16.1]# ./configure --prefix=/opt/nginx-1.16 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module   #编译配置
[root@web01 nginx-1.16.1]# echo $?                   #0代表上一步命令执行成功
0
[root@web01 nginx-1.16.1]# make && make install        #编译过程和编译安装
[root@web01 nginx-1.16.1]# echo $?
0
[root@web01 nginx-1.16]# ln -s /opt/nginx-1.16 /opt/nginx #创建软连接,为了方便以后升级
[root@web01 conf]# cd /opt/nginx/conf/
精简配置文件
[root@web01 opt]# cd nginx-1.16.1/conf                #最有用的配置文件有22行
[root@web01 conf]# grep -Ev "#|^$" nginx.conf.default >nginx.conf   
[root@web01 conf]# vim nginx.conf       #修改nginx配置文件,增加3个web网站
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  www.81.com;
 13         location / {
 14             root   html/www;
 15             index  index.html index.htm;
 16         }
 17     }
 18     server {
 19         listen       80;
 20         server_name  bbs.81.com;
 21         location / {
 22             root   html/bbs;
 23             index  index.html index.htm;
 24         }
 25     }
 26     server {
 27         listen       80;
 28         server_name  ftp.81.com;
 29         location / {
 30             root   html/ftp;
 31             index  index.html index.htm;
 32         }
 33     }
 34 }
[root@web01 conf]# mkdir /opt/nginx/html/{www,bbs,ftp}  -p   #创建3个web目录
[root@web01 conf]# for name in www bbs ftp;do echo "$(hostname) $name.81.com" >/opt/nginx/html/$name/index.html;done                    #批量创建3个index文件
[root@web01 conf]# for name in www bbs ftp;do cat /opt/nginx/html/$name/index.html;done
web01 www.81.com                                     #批量查询
web01 bbs.81.com
web01 ftp.81.com
C:\Windows\System32\drivers\etc\hosts               #window中增加DNS解析
10.14.21.81  www.81.com  bbs.81.com  ftp.81.com
10.14.21.82  www.81.com  bbx.81.com  ftp.81.com
[root@web01 conf]# vim /etc/hosts                   #linux服务器增加DNS解析
10.14.21.81  www.81.com  bbs.81.com  ftp.81.com
10.14.21.82  www.81.com  bbx.81.com  ftp.81.com
[root@web01 conf]# /opt/nginx/sbin/nginx             #启动nginx服务
①、浏览器分别输入www.81.com、bbs.81.com、ftp.81.com检查是否正常显示web页面
②、linux中测试是否显示web页面
[root@web01 ~]# curl http://www.81.com:80
web01 www.81.com
[root@web01 ~]# curl http://bbs.81.com:80
web01 bbs.81.com
[root@web01 ~]# curl http://ftp.81.com:80
web01 ftp.81.com

二、配置web02:
部署nginx过程同上。

[root@web01 conf]# scp -rp /opt/nginx/conf/nginx.conf 10.14.21.82:/opt/nginx/conf/
[root@web02 conf]# mkdir /opt/nginx/html/{www,bbs,ftp}  -p
[root@web02 conf]# cd
[root@web02 ~]# for name in www bbs ftp;do echo "$(hostname) $name.81.com" >/opt/nginx/html/$name/index.html;done
[root@web02 ~]# for name in www bbs ftp;do cat /opt/nginx/html/$name/index.html;done
web02 www.81.com
web02 bbs.81.com
web02 ftp.81.com
[root@web02 ~]# /opt/nginx/sbin/nginx
①、浏览器分别输入www.81.com、bbs.81.com、ftp.81.com检查是否正常显示web页面
②、linux中测试是否显示web页面
[root@web02 ~]# curl http://www.81.com
web02 www.81.com
[root@web02 ~]# curl http://bbs.81.com
web02 bbs.81.com
[root@web02 ~]# curl http://ftp.81.com
web02 ftp.81.com

三、在负载均衡服务器lb01上访问测试

[root@lb01 conf]# curl 10.14.21.81/index.html
web01 www.81.com
[root@lb01 conf]# curl -H host:www.81.com 10.14.21.81/index.html
web01 www.81.com
[root@lb01 conf]# curl -H host:bbs.81.com 10.14.21.81/index.html
web01 bbs.81.com
[root@lb01 conf]# curl -H host:ftp.81.com 10.14.21.81/index.html
web01 ftp.81.com
[root@lb01 conf]# curl -H host:www.81.com 10.14.21.82/index.html
web02 www.81.com
[root@lb01 conf]# curl -H host:bbs.81.com 10.14.21.82/index.html
web02 bbs.81.com
[root@lb01 conf]# curl -H host:ftp.81.com 10.14.21.82/index.html
web02 ftp.81.com
以上cur -H,就不用再配置hosts中DNS解析

四、在lb01配置反向代理服务(增加upstream和proxy_pass两个模块)

[root@lb01 conf]# cd /opt/nginx/conf/
[root@lb01 conf]# vim nginx.conf
#1、upstream模块常用功能
#weight:实现权重值负载访问功能,能力强干的多。
#max_fails:定义后端访问的失败次数-max_fails 健康检查,3次以后就放弃,默认一次
#fail_timeout:定义后端失败重试的间隔-fail_timeout。等待10秒才能再发给你
#backup:热备节点。其他服务器都坏的时候,次服务器才会启用
#2、upstream常用调度算法
# rr:     定义轮询调度算法,采取平均分配,默认调度算法
# wrr:   定义权重调度算法,能者多劳。
# ip_hash:定义静态调度算法,此算法一定不能和backup与weight参数同时出现
# least_conn:定义最小的连接数,谁的连接数小就多分配给他,谁的多就不要再给他压力了
#3、proxy模块常用功能
# proxy_set_header:设置反向代理服务器到web服务器的HTTP请求报文中的头部信息
#用户访问bbs时,lb向server请求的是upstream里面的cncc地址池名称
#用地址池名称访问等价于用ip地址访问bbs站点,默认显示第一个虚拟主机www
#解决方案:更改请求头信息为域名bbs
# proxy_set_header X-Forwarded-For $remote_addr  日志最后显示真正客户端IP地址

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream cnnc {           # 管理哪些服务器,定义一个地址池
    # ip_hash;            #固定访问某个服务器
# least_conn;
        server 10.14.21.81:80 weight=3 max_fails=3 fail_timeout=10s;     
        server 10.14.21.82:80 weight=1 max_fails=3 fail_timeout=10s;
    }
    server {
        listen       80;
        server_name  localhost;
        root   html;
        index  index.html index.htm;
        location / {
          proxy_pass http://cnnc;      #固定写法
      #proxy_set_header host $host;   #$host表示客户端反向代理的请求
      #proxy_set_header X-Forwarded-For $remote_addr;  #显示真正客户端IP
#proxy_pass主要用于进行抛送用户访问请求给upstream模块中的相应节点服务器       
}   
    }
}
[root@lb01 conf]# /opt/nginx/sbin/nginx                #启动nginx
注意:有负载均衡服务器时,hosts解析文件中的ip要改为lb01的地址
C:\Windows\System32\drivers\etc\hosts                #window中增加DNS解析
#10.14.21.81  www.81.com  bbs.81.com  ftp.81.com   #注释掉之前的hosts文件
#10.14.21.82  www.81.com  bbs.81.com  ftp.81.com   #注释掉之前的hosts文件
10.14.21.86  www.81.com  bbs.81.com  ftp.81.com    #86为lb01
10.14.21.87  www.81.com  bbs.81.com  ftp.81.com    #87位lb02
①、浏览器分别输入www.81.com、bbs.81.com、ftp.81.com检查是否正常显示负载均衡web页面
②、linux中测试是否显示web页面
[root@lb01 conf]# curl -H host:www.81.com 10.14.21.86/index.html
web01 www.81.com
[root@lb01 conf]# curl -H host:www.81.com 10.14.21.86/index.html
web02 www.81.com                                        #已实现访问负载均衡

五、安装keepalive服务-
1、配置lb01
[root@lb01 conf]# scp -rp nginx.conf 10.14.21.87:/opt/nginx/conf/ #将lb01的配置发给lb02
[root@lb01 conf]# yum -y install keepalived #安装keepalive
[root@lb01 conf]# vim /etc/keepalived/keepalived.conf #修改配置文件
配置文件结构:
#GLOBAL CONFIGURATION — 全局配置(
#VRRPD CONFIGURATION — vrrp配置(

#LVS CONFIGURATION — LVS服务相关配置
! Configuration File for keepalived
global_defs { #全局配置
router_id lb01 #尽量用主机名表示
}

vrrp_instance gorup01 { #vrrp的实例:家族的名字是group01,主备节点实例名必须相同
state MASTER #描述实例中主备角色状态(只起到说明作用)
interface ens192 #设置虚ip地址放置网卡的位置(心跳ip存在的方位)
virtual_router_id 51 #家族的标识
priority 150 #谁的大,谁就是主 重要
advert_int 1 #主每1秒发送一次组播包来告诉备,不要篡权
authentication { #认证:暗号。提高安全性
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #配置心跳地址,设备、存放网卡、标签信息-第一个虚拟ip
10.14.21.80/24 dev ens192 label ens192:1
}
}
2、配置lb02服务器
Lb02配置文件,只有以上3个红色部分不同,其他都一致
[root@lb01 conf]# systemctl restart keepalived #启动lb01
[root@lb02 conf]# systemctl restart keepalived #启动lb02
3、检查
[root@lb01 conf]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:50:56:84:5f:20 brd ff:ff:ff:ff:ff:ff
inet 10.14.21.86/24 brd 10.14.21.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet 10.14.21.80/24 scope global secondary ens192:1
valid_lft forever preferred_lft forever
#以上已经生成心跳ip地址
只有主keepalive服务有心跳地址。备服务器没有。
只有当主DOWN时,备就会升级到主
不管主还是备当机,用户访问的都是 10.14.21.80这个地址。
所以hosts文件解析地址应改为心跳地址10.14.21.80,

六、案例:根据淘宝网站URI信息抛送给相应服务器
用户请求URI 请求服务器 站点目录 功能
如:/fruit 10.1.1.1 www.81.com/www/fruit 水果服务器
/book 10.1.1.2 www.81.com/www/book 图书服务器
/ 10.1.1.3 www.81.com/www/electron 默认
1、分别在在2台主机上创建各自目录,并分别生成网站页面
[root@TEST01 ~]# cd /opt/nginx/html/www/
[root@TEST01 www]# mkdir fruit
[root@TEST01 www]# echo info fruit >fruit/index.html #生成网站页面
[root@TEST01 www]# cat fruit/index.html
info fruit
[root@TEST02 ~]# cd /opt/nginx/html/www/
[root@TEST02 www]# mkdir book
[root@TEST02 www]# echo info fruit >book/index.html #生成网站页面
[root@TEST02 www]# cat book/index.html
info book
2、访问测试
[root@lb01 ~]# curl -H host:www.one.com 10.1.1.1/fruit/index.html
info fruit
[root@lb01 ~]# curl -H host:www.one.com 10.1.1.2/book/index.html
info book
[root@lb01 ~]# curl -H host:www.one.com 10.1.1.3/index.html
info www.one.com
3、lb01配置
[root@lb01 conf]# cd /opt/nginx/conf/
[root@lb01 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream fruit {
server 10.1.1.1:80;
}
upstream book {
server 10.1.1.2:80;
}
upstream default {
server 10.1.1.3:80;
}
server {
listen 80;
server_name www.one.com;
root html;
index index.html index.htm;
location /fruit {
proxy_pass http://fruit;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /book {
proxy_pass http://book;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
proxy_pass http://default;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}

七、案例:根据不同设备抛送不同web页面
根据案例6,只需要配置负载均衡服务器即可
[root@lb01 conf]# cd /opt/nginx/conf/
[root@lb01 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream iphone {
server 10.1.1.1:80;
}
upstream android {
server 10.1.1.2:80;
}
upstream pc {
server 10.1.1.3:80;
}
server {
listen 80;
server_name www.one.com;
root html;
index index.html index.htm;
location / {
if ( h t t p u s e r a g e n t   ∗ " i p h o n e " ) p r o x y p a s s h t t p : / / i p h o n e ; i f ( http_user_agent ~* "iphone") { proxy_pass http://iphone; } if ( httpuseragent "iphone")proxypasshttp://iphone;if(http_user_agent ~* “android”) {
proxy_pass http://android;
}
proxy_pass http://pc; #默认发送至PC
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
} } }
手机端测试,按F12后选择toggle device toolbar便可测试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值