5. Nginx七层负载均衡的配置、七层负载均衡的作用

1.Nginx负载均衡基本概述

  1. 为什么要是用负载均衡
当我们的web服务器直接面向用户,往往要承受大量的并发请求,
单台服务器难以负荷,我使用多台web服务器组成集群,前端使用Nginx负载均衡,
将请求分散的打到我们的后端集群中,实现负载的分支.
那么会大大的提升系统的吞吐率,请求性能,高容宰.

往往我们就出的最多的是SLB(Server Load Balance)负载均衡,
实现最多的也是SLB,那么SLB他的调度节点和服务节点通常实在一个地域里面.
那么它在这个小得逻辑地域里面决定了对部分服务的实时性,响应性是非常好的.

所以说当海量用户请求过来以后,他同样是请求调度节点,
调度节点将用户的请求转发给后端对应的服务节点,
服务节点处理完请求后宰转发给调度节点,调度节点最后响应给用户节点,
这样实现一个均衡的作用,那么Nginx是一个典型的SLB
  1. 负载均衡的叫法:
负载均衡
负载
Load Balance
LB
云叫法:
SLB 阿里云负载均衡
QLB 青云负载均衡
CLB 腾讯云负载均衡
ULB UClound负载均衡
  1. 常见的负载均衡的软件
Nginx
Haproxy
LVS
  1. 负载均衡的应用场景一:四层负载
所谓四层负载均衡是指OSI七层模型中的传输层,
那么传输层Nginx已经支持TCP/IP的控制,
所以只需要对客户端的请求进行TCP/IP协议的包转发就可以实现负载,
那么他的好处是性能非常快,
只需要底层进行应用处理,而不需要进行一些复杂的逻辑.
  1. 负载均衡的应用场景二:七层负载
七层负载均衡是在应用层,那么他可以完成后很多应用方面的协议请求,
当然四层有四层的好处,七层七层的好处,四层就不支持协议的转发,(http,https,DNS等)只支持IP,但是它的速度快.
应用层虽然没有四层负载快,但是支持很多功能,比如说他支持http信息的改写、头部信息的改写、(意识是,七层代理着用户往后请求的时候把我们用户请求的头部信息加上,长连接协议也可以修改等)、
安全应用规则控制、URL匹配规则控制、以及转发、rewrite等一些规则,
所以在应用层的服务里面,可以做的内容就更多了。Nginx是一个典型的七层负载均衡
  1. 四层负载均衡与七层负载均衡的区别:
四层负载均衡数据包在底层就进行了转发,而七层负载均衡数据则是在最顶层进行转发,
由此可以看出,七层负载效率没有四层负载均衡效率高。
但是七层负载均衡更贴近与服务,如:http就是七层协议,我们可以用Nginx做回话保持,
URL路劲规则匹配、头部改写等等,这些四层是无法实现的

注意:四层负载均衡不识别域名,七层负载均衡识别域名


2.Nginx负载均衡匹配场景

  • Nginx要实现负载均衡需要用到proxy_pass代理模块配置

  • Nginx负载均衡与nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器,(可以说是后端的一个location一个匹配规则,你想要访问/admin,那我给你抛到后端的那台服务器下的某一个位置中去),而Nginx负载均衡是将客户端请求代理转发至一组upstream虚拟服务池。(反向代理一个location只能匹配后端的一个server,那我们的负载均衡就不一样了,用定义的方式把后端的服务放到一个组里面,使用这个组需要一个模块,upstream就是来定义地址池的,也就是说我先把后端的所有你想要负载的节点,我给你定义到一个地址池里面去这个地址池叫upstream,upstream存放着所有的地址。当用户再去请求server_name时,就不是请求一个服务了,而是直接请求一个组名就可以了,uptream默认会轮训匹配,一个一个的放下找,直到找到想要的地址为止。负载均衡有两个模块,一个是proxy_pass模块、一个是upstream模块)

Nginx upstream虚拟配置语法及用法解释

upstream
upstream name {                 #名字可以随便起
    server www.xxx.com;         #后端的域名
    server www.xxx.com:666;     #后端的端口
}
server {
    location / {
        proxy_pass http://name; #proxy_pass调用upstream里定义的名字
    }
}
环境准备
角色外网IP(NRT)内网IP(LAN)主机名
LB01eth0:172.16.1.5eth1:172.16.1.5lb01
web01eth0:172.16.1.7eth1:172.16.1.7web01
web02eth0:172.16.1.8eth1:172.16.1.8web02

3.nginx七层负载均衡配置

  1. 配置lb01服务文件
root@lb01 ~]# vim /etc/nginx/conf.d/lb.conf
upstream wordpress.sheng.com {
    server 172.16.1.7;
    server 172.16.1.8;
}
upstream zh.sheng.com {
    server 172.16.1.7;
    server 172.16.1.8;
}
upstream phpshe.sheng.com {
    server 172.16.1.7;
    server 172.16.1.8;
}
server {
    listen 80;
    server_name wordpress.sheng.com;

    location / {
        proxy_pass http://wordpress.sheng.com;
	proxy_set_header Host $http_host;
	proxy_http_version 1.1;
	proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}

server {
    listen 80;
    server_name phpshe.sheng.com;

    location / {
        proxy_pass http://phpshe.sheng.com;
	proxy_set_header Host $http_host;
	proxy_http_version 1.1;
	proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}
server {
    listen 80;
    server_name zh.sheng.com;

    location / {
        proxy_pass http://zh.sheng.com;
	proxy_set_header Host $http_host;
	proxy_http_version 1.1;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}

  1. 准备Nginx负载均衡调度使用的proxy_params
[root@lb01 ~]# cat /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;

proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

4.Nginx负载均衡回话保持

  1. 在web01上配置nginx
[root@web01 ~]# cat /etc/nginx/conf.d/php.conf
server {
    listen 80;
    server_name php.sheng.com;
    root /admin;
    index index.php index.html;

        location ~ \.php$ {
        root /zh;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }
}
  1. 检查配置是否正确,并重新加载nginx服务
[root@web01 ~]# cat /etc/nginx/conf.d/php.conf
server {
    listen 80;
    server_name php.sheng.com;
    root /admin;
    index index.php index.html;

        location ~ \.php$ {
        root /admin;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }
}
[root@web01 ~]# systemctl reload nginx
  1. 下载
[root@web01 ~]# wget https://files.phpmyadmin.net/phpMyAdmin/5.1.0/phpMyAdmin-5.1.0-all-languages.zip
  1. 解压,根据配置文件的需求,配置所需
[root@web01 ~]# unzipphpMyAdmin-5.1.0-all-languages.zip #解压
[root@web01 ~]# mv phpMyAdmin-5.1.0-all-languages /usr/local #移动
[root@web01 ~]# ln -s /usr/local/phpMyAdmin-5.1.0-all-languages/ /admin #做软连接
[root@web01 ~]# ll -d /admin
lrwxrwxrwx 1 root root 42 Mar 27 16:31 /admin -> /usr/local/phpMyAdmin-5.1.0-all-languages/  # 查看
  1. 授权
[root@web01 ~]# chown -R www.www /var/lib/php/session
  1. 修改数据库用户名,先复制
[root@web01 admin]# cp config.sample.inc.php config.inc.php
[root@web01 admin]# vim config.inc.php
/* Server parameters */
$cfg['Servers'][$i]['host'] = '172.16.1.51';  #修改
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;

在web02上配置myadmin

  1. 推送web01上的配置文件到web02上
[root@web01 admin]# scp /etc/nginx/conf.d/php.conf 172.16.1.8:/etc/nginx/conf.d/
[root@web01 admin]# scp -r /usr/local/phpMyAdmin-5.1.0-all-languages/ 172.16.1.8:/usr/local/
  1. 配置nginx文件的需求,做软连接
[root@web02 conf.d]# ln -s /usr/local/phpMyAdmin-5.1.0-all-languages/ /admin
  1. 检查nginx是否正确,并重启
[root@web02 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 conf.d]# systemctl restart nginx
  1. 授权
[root@web02 conf.d]# chown -R www.www /var/lib/php/session

在lb01服务器上配置文件

1.配置lb.conf,添加php.sheng.com

[root@lb01 conf.d]# vim lb.conf
upstream wordpress.shengcom {
    server 172.16.1.7;
    server 172.16.1.8;
}
upstream zh.sheng.com {
    server 172.16.1.7;
    server 172.16.1.8;
}
upstream phpshe.sheng.com {
    server 172.16.1.7;
    server 172.16.1.8;
}
upstream php.sheng.com {
    server 172.16.1.7;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name wordpress.sheng.com;

    location / {
        proxy_pass http://wordpress.sheng.com;
	proxy_set_header Host $http_host;
	proxy_http_version 1.1;
	proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}

server {
    listen 80;
    server_name phpshe.sheng.com;

    location / {
        proxy_pass http://phpshe.sheng.com;
	proxy_set_header Host $http_host;
	proxy_http_version 1.1;
	proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}
server {
    listen 80;
    server_name zh.sheng.com;

    location / {
        proxy_pass http://zh.sheng.com;
	proxy_set_header Host $http_host;
	proxy_http_version 1.1;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}
server {
    listen 80;
    server_name php.sheng.com;

    location / {
        proxy_pass http://php.sheng.com;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
    }
}
  1. 检查并重启nginx
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl restart nginx
  1. 在db01数据库上安装Redis数据库
[root@db01 ~]# yum -y install redis
  1. 配置Redis的文件,并重启
[root@db01 ~]# vim /etc/redis.conf 
bind 127.0.0.1 172.16.1.51
  1. 在web02上安装Redis
[root@web02 conf.d]# yum -y install redis
  1. 在web02上测试是否能远程连接
[root@web02 conf.d]# redis-cli -h 172.16.1.51
172.16.1.51:6379> 
  1. 在web01上修改/etc/php.ini文件
[root@web01 ~]# vim /etc/php.ini
session.save_handler = redis
session.save_path = "tcp://172.16.1.51:6379"
;session.save_path = "tcp://172.16.1.51:6379?auth=123" #如果redis存在密码,则使用该方式
session.auto_start = 1
  1. 在web01上注释掉/etc/php-fpm.d/www.conf文件中的一下两条内容
;php_value[session.save_handler] = files
;php_value[session.save_path]    = /var/lib/php/session
  1. 重启php-fpm
[root@web01 session]# systemctl restart php-fpm
  1. 将配置号的文件推送到web02上
[root@web01 ~]# scp /etc/php.ini root@172.16.1.8:/etc/php.ini  
[root@web01 ~]# scp /etc/php-fpm.d/www.conf root@172.16.1.8:/etc/php-fpm.d/www.conf 
  1. 上web02上重启php-fpm
[root@web02 session]# systemctl restart php-fpm
  1. redis查看数据
[root@db01 ~]# redis-cli 
127.0.0.1:6379> keys *
1) "PHPREDIS_SESSION:e1a4e117d2f30f17535e80fecc366043"
2) "PHPREDIS_SESSION:888faf20779bfb887f3188eca012baa4"
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值