12.3负载均衡介绍、负载均衡实践、负载均衡调度算法、后端状态,Nginx负载均衡会话保持/12.4四层负载均衡

本文详细介绍了负载均衡的概念、Nginx负载均衡的配置与实践,包括轮询、权重轮询、健康检查、会话保持等内容,以及在WordPress和PHP项目中的应用实例,涉及四层和七层负载均衡的比较和区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言

一、负载均衡介绍

1.什么是负载均衡

将请求平均的分配给后端服务器

2.为什么要使用负载均衡

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

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

所以说当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx则是一个典型的SLB
在这里插入图片描述

3.负载均衡的叫法

1.负载均衡
2.负载
3.LB
4.Load Balance

4.公有云中常见的负载均衡

1.SLB #阿里云产品
2.LB #青云产品
3.CLB #腾讯云产品
4.ULB #Ucloud产品

5.负载均衡软件

1.nginx
2.Haproxy
3.LVS

6.负载均衡类型

1.四层负载均衡
所谓四层负载均衡指的是OSI七层模型中的传输层,那么传输层Nginx已经能支持TCP/IP的控制,所以只需要对客户端的请求进行TCP/IP协议的包转发就可以实现负载均衡,那么它的好处是性能非常快、只需要底层进行应用处理,而不需要进行一些复杂的逻辑

2.七层负载均衡
七层负载均衡它是在应用层,那么它可以完成很多应用方面的协议请求,比如我们说的http应用的负载均衡,它可以实现http信息的改写、头信息的改写、安全应用规则控制、URL匹配规则控制、以及转发、rewrite等等的规则,所以在应用层的服务里面,我们可以做的内容就更多,那么Nginx则是一个典型的七层负载均衡SLB

3.四层和七层负载均衡的区别
四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发、由此可以看出,七层负载均衡效率没有四负载均衡高。
但七层负载均衡更贴近于服务,如:http协议就是七层协议,我们可以用Nginx可以作会话保持,URL路径规则匹配、head头改写等等,这些是四层负载均衡无法实现的。
注意:四层负载均衡不识别域名,七层负载均衡识别域名

二、负载均衡实践

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

Nginx负载均衡与Nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池.

1.负载均衡模块
# ngx_http_upstream_module

#语法
Syntax:	upstream name { ... }
Default:	—
Context:	http

#例子
upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
	... ...
    location / {
        proxy_pass http://backend;
    }
}

正文

一、负载均衡

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

Nginx负载均衡与Nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池.

1.负载均衡模块
# ngx_http_upstream_module

#语法
Syntax:	upstream name { ... }
Default:	—
Context:	http

#例子
upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {
	... ...
    location / {
        proxy_pass http://backend;
    }
}
2.环境准备

在这里插入图片描述

3.web01准备站点

1)配置nginx

[root@web01 ~]# vim /etc/nginx/conf.d/linux.lb.com.conf
server {
    listen 80;
    server_name linux.lb.com;
    charset utf8;

    location / {
        root /code/lb;
        index index.html;
    }
}

2)准备网站

[root@web01 ~]# mkdir /code/lb
[root@web01 ~]# echo "我是web0111111111111" > /code/lb/index.html
[root@web01 ~]# chown -R www.www /code/

3)配置hosts访问

[root@web01 ~]# systemctl restart nginx

10.0.0.7 linux.lb.com

访问
4.配置负载均衡配置文件
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.lb.com.conf
upstream web_group {
    server 172.16.1.7:80;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name linux.lb.com;

    location / {
        proxy_pass http://web_group;
        include proxy_params;
    }
}

}

5.准备代理优化文件
[root@lb01 ~]# vim /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 10s;
proxy_read_timeout 10s;
proxy_send_timeout 10s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
6.重启访问测试
1.重启
[root@lb01 ~]# 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 ~]# systemctl restart nginx

2.配置hosts
10.0.0.4 linux.lb.com

在这里插入图片描述

二、负载均衡结合项目

1.配置wordpress负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf
upstream blog {
    server 172.16.1.7;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name linux.wp.com;

    location / {
        proxy_pass http://blog;
        include proxy_params;
    }   
}

#重启
[root@lb01 ~]# systemctl restart nginx
2.配置hosts查看网站
10.0.0.4 linux.wp.com
3.配置zh的负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.zh.com.conf
upstream zh {
    server 172.16.1.7;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name linux.zh.com;

    location / {
        proxy_pass http://zh;
        include proxy_params;
    }
}
4.负载均衡常见问题

1)问题
如果后端服务器出现问题,负载均衡仍然会将请求发送给相应机器

如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点down掉的时候,Nginx会更据你具体负载均衡的设置,将请求转移到其他的节点上,但是,如果后台服务连接没有down掉,但是返回错误异常码了如:504、502、500,这个时候你需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,当其中一台返回错误码404,500…等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率。
2)解决问题的模块

Syntax:	proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default:	proxy_next_upstream error timeout;
Context:	http, server, location

3)配置

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf
upstream blog {
    server 172.16.1.7;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name linux.wp.com;

    location / {
        proxy_pass http://blog;
        include proxy_params;
        #可以配置,可以写到include
        proxy_next_upstream http_500 | http_502 | http_503 | http_504 | http_403 | http_404;
    }
}

#重启
[root@lb01 ~]# systemctl restart nginx

三、负载均衡调度算法

在这里插入图片描述
1.轮询的配置方法

upstream blog {
    server 172.16.1.7;
    server 172.16.1.8;
}
2.加权轮询配置
upstream blog {
    server 172.16.1.7 weight=1;
    server 172.16.1.8 weight=2;
}
3.ip_bash配置方法
upstream web_group {
    server 172.16.1.7:80;
    server 172.16.1.8;
    ip_hash;
}

#一般用来做会话保持

四、负载均衡后端状态

在这里插入图片描述

1.down状态配置 (更新的时候使用)
upstream blog {
	#一般情况下,维护场景使用
    server 172.16.1.7 down;
    server 172.16.1.8;
}
2.backup状态配置 (放长假期中不想解决问题,未雨绸缪,备份的服务器
upstream web_group {
    server 172.16.1.7;
    server 172.16.1.8 backup;
}
3.访问错误状态配置 (最大错误次数3次,每次间隔10秒)
upstream web_group {
    server 172.16.1.7;
    server 172.16.1.8 max_fails=3 fail_timeout=10s;
}
4.最大连接数配置
upstream web_group {
    server 172.16.1.7;
    server 172.16.1.8 max_conns=100;
}

五、Nginx负载均衡健康检查(扩展)

在Nginx官方模块提供的模块中,没有对负载均衡后端节点的健康检查模块,但可以使用第三方模块。
nginx_upstream_check_module来检测后端服务的健康状态。

1.安装依赖
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2.下载第三方模块
[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
3.解压及安装
[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb02 ~]# unzip master.zip
4.进入nginx目录,打补丁(nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录)
[root@lb02 ~]# cd nginx-1.14.2/

[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch

[root@lb02 nginx-1.14.2]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

[root@lb02 nginx-1.14.2]# make && make install
[root@lb02 ~]# cd nginx-1.14.2/

[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch

[root@lb02 nginx-1.14.2]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

[root@lb02 nginx
5.在已有的负载均衡上增加健康检查的功能
[root@lb01 conf.d]# cat proxy_web.conf
upstream web {
    server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
    server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
    check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
    #interval  检测间隔时间,单位为毫秒
    #rise      表示请求2次正常,标记此后端的状态为up
    #fall      表示请求3次失败,标记此后端的状态为down
    #type      类型为tcp
    #timeout   超时时间,单位为毫秒
}

server {
    listen 80;
    server_name linux.lb.com;
    location / {
        proxy_pass http://web;
        include proxy_params;
    }

    location /upstream_check {
        check_status;
    }
}
6.重启访问页面
#重启
#配置hosts
#访问
http://linux.lb.com/upstream_check

六、Nginx负载均衡会话保持(不需要重新登录)

1.什么是会话保持

我们在访问网站的时候,进行登陆以后,服务器上回生成一个session,然后服务器会携带着session_id返回给浏览器记录一个cookie值,当第二次访问时,cookie会来服务器上与session进行对比,如果对比成功,则不需要重新登录

在使用负载均衡的时候会遇到会话保持的问题,可通过如下方式进行解决。
1.使用nginx的ip_hash,根据客户端的IP,将请求分配到对应的IP上
2.基于服务端的session会话共享(NFS,MySQL,memcache,redis,file)

2.session共享的方法

1.把多台后端服务器session文件目录挂载到NFS同一目录
2.通过程序将session存储到mysql数据库
3.通过程序将session存储到redis缓存

3.web01上搭建phpmyadmin

1)上传包

[root@web01 ~]# cd /code/
[root@web01 code]# rz                                                                               
[root@web01 code]# ll
-rw-r--r--. 1 root root 11060845 Oct 18 10:48 phpMyAdmin-4.9.0.1-all-languages.zip

2)解压并改名

[root@web01 code]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
[root@web01 code]# mv phpMyAdmin-4.9.0.1-all-languages php

3)修改连接数据库代码

[root@web01 code]# cp /code/php/{config.sample.inc.php,config.inc.php}
[root@web01 code]# vim /code/php/config.inc.php
$cfg['Servers'][$i]['host'] = '172.16.1.51';

4)授权站点目录

[root@web01 code]# chown -R www.www /code/

5)配置nginx

[root@web01 ~]# vim /etc/nginx/conf.d/linux.php.com.conf
server {
    listen 80;
    server_name linux.php.com;
    root /code/php;

    location / {
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

[root@web01 ~]# systemctl restart nginx

6)配置hosts访问

#配置hosts
10.0.0.7 linux.php.com

#访问页面  http://linux.php.com/

#报错:
Error during session start; please check your PHP and/or webserver log file and configure your PHP installation properly. Also ensure that cookies are enabled in your browser.
session_start(): open(SESSION_FILE, O_RDWR) failed: Permission denied (13)
session_start(): Failed to read session data: files (path: /var/lib/php/session)

#解决:
[root@web01 ~]# chown -R www.www /var/lib/php/session

7)再次登录测试

#登录测试,如果登录用户密码错误
mysqli_real_connect(): (HY000/1045): Access denied for user 'root'@'172.16.1.7' (using password: YES)

#数据库授权  (db01)
[root@db01 ~]# mysql -uroot -p123   #先进入数据库
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all on *.* to root@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
4.web02同步web01

1)同步站点文件

[root@web01 ~]# scp -r /code/php 172.16.1.8:/code/

2)同步nginx配置

[root@web01 ~]# scp /etc/nginx/conf.d/linux.php.com.conf 172.16.1.8:/etc/nginx/conf.d/

3)授权目录

[root@web01 ~]# chown -R www.www /var/lib/php/svim ession
[root@web02 ~]# chown -R www.www /code/

4)访问测试

#重启
[root@web02 ~]# systemctl restart nginx

#配置hosts
10.0.0.8 linux.php.com

#访问
http://linux.php.com/index.php

5.配置负载均衡

1)配置

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.php.com.conf
upstream phpmyadmin {
    server 172.16.1.7;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name linux.php.com;

    location / {
        proxy_pass http://phpmyadmin;
        include proxy_params;
    }
}

[root@lb01 ~]# 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 ~]# systemctl restart nginx

2)访问

#配置hosts
10.0.0.4 linux.php.com

#访问报错
Failed to set session cookie. Maybe you are using HTTP instead of HTTPS to access phpMyAdmin.
6.解放决方式一:挂载session文件的目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/wp /var/lib/php/session/
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/wp /var/lib/php/session/
7.解决方式二:使用redis实现session共享

1)安装redis

[root@db01 ~]# yum install -y redis

2)配置服务

[root@db01 ~]# vim /etc/redis.conf
bind 172.16.1.51

3)启动服务

[root@db01 ~]# systemctl start redis

#检查
[root@db01 ~]# netstat -lntp
tcp        0      0 172.16.1.51:6379        0.0.0.0:*               LISTEN      13305/redis-server

4)修改PHP服务将session存储到redis

[root@web01 ~]# vim /etc/php.ini   
#原配置
#session.save_handler = files
session.save_handler = redis
#;session.save_path = "/tmp"
session.save_path = "tcp://172.16.1.51:6379"

[root@web01 ~]# vim /etc/php-fpm.d/www.conf
#注释以下两行
;php_value[session.save_handler] = files
;php_value[session.save_path]    = /var/lib/php/session

5)重启

[root@web01 ~]# systemctl restart php-fpm

6)再次访问测试

linux.php.com

7)redis验证session

1.连接进入redis
[root@db01 ~]# redis-cli -h 172.16.1.51

2.查看所有key
172.16.1.51:6379> keys *

3.查看session的生存时间
172.16.1.51:6379> TTL PHPREDIS_SESSION:43948cd72f7589982cc3758f9d5c2b8d
(integer) 1265

4.退出
172.16.1.51:6379> quit

一、四层负载均衡

1.什么是四层负载均衡
所谓四层负载均衡,也就是主要通过报文中的目标地址和端口,再加上负载均衡设备设置的服务器选择方式,决定最终选择的内部服务器。

以常见的TCP为例,负载均衡设备在接收到第一个来自客户端的SYN 请求时,选择一个最佳的服务器,并对报文中目标IP地址进行修改(改为后端服务器IP),直接转发给该服务器。TCP的连接建立,即三次握手是客户端和服务器直接建立的,负载均衡设备只是起到一个类似路由器的转发动作。在某些部署情况下,为保证服务器回包可以正确返回给负载均衡设备,在转发报文的同时可能还会对报文原来的源地址进行修改。
在这里插入图片描述
在这里插入图片描述

2.应用场景

1.四层+七层来做负载均衡,四层可以保证七层的负载均衡的高可用性;
2.负载均衡可以做端口转发
3.数据库读写分离

3.四层负载均衡特点

1.四层负载均衡仅能转发TCP/IP协议、UDP协议、通常用来转发端口,如:tcp/22、udp/53;
2.四层负载均衡可以用来解决七层负载均衡端口限制问题;(七层负载均衡最大使用65535个端口号)
3.四层负载均衡可以解决七层负载均衡高可用问题;(多台后端七层负载均衡能同时的使用)
4.四层的转发效率比七层的高得多,但仅支持tcp/ip协议,不支持http和https协议;
5.通常大并发场景通常会选择使用在七层负载前面增加四层负载均衡。

二、四层负载均衡实践

1.环境准备

在这里插入图片描述

2.测试lb01

lb01负载均衡确认没有问题

3.lb4和lb02搭建nginx

1.配置yum源
2.安装
3.配置nginx
4.创建用户
5.启动

1.[root@lb4 ~]# history

  ll /etc/yum.repos.d/nginx.repo 
    2  yum install -y nginx
    3  vim /etc/nginx/nginx.conf 
    4  groupadd www -g 666
    5  useradd www -u 666 -g 666 -s /sbin/nologin -M
    6  systemctl start nginx
2.[root@lb02 ~]# history
    1  ll /etc/yum.repos.d/nginx.repo 
    2  yum install -y nginx
    3  vim /etc/nginx/nginx.conf 
    4  groupadd www -g 666
    5  useradd www -u 666 -g 666 -s /sbin/nologin -M
    6  systemctl start nginx
3.[root@lb01 ~]# history

  60  scp /etc/nginx/conf.d/* 172.16.1.5:/etc/nginx/conf.d/
   61  scp /etc/nginx/proxy_params 172.16.1.5:/etc/nginx/

4.将lb01配置同步到lb02
[root@lb01 ~]# scp /etc/nginx/conf.d/* 172.16.1.5:/etc/nginx/conf.d/
[root@lb01 ~]# scp /etc/nginx/proxy_params 172.16.1.5:/etc/nginx/
5.测试lb02的负载均衡
[root@lb02 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb02 ~]# systemctl restart nginx

#配置hosts测试
10.0.0.5 linux.wp.com
6.配置四层负载均衡

1)四层负载均衡语法

Syntax:	stream { ... }
Default:	—
Context:	main

#示例:四层负载均衡stream模块跟http模块在同一级别,不能配置在http里面
stream {
    upstream backend {
        server backend1.example.com:12345 weight=5;
        server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;
    }

    server {
        listen 12345;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }
}

2)配置nginx主配置文件

[root@lb4 ~]# vim /etc/nginx/nginx.conf
#注释http层所有内容
user  www;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
#添加一个包含文件
include /etc/nginx/conf.c/*.conf;
#http {
#    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;
#}

3)配置四层负载均衡

#创建目录
[root@lb4 ~]# mkdir /etc/nginx/conf.c

#配置
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
    upstream lbserver {
        server 10.0.0.4:80;
        server 10.0.0.5:80;
    }

    server {
        listen 80;
        proxy_pass lbserver;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
    }
}

4)启动服务

[root@lb4 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb4 ~]# systemctl start nginx

5)配置hosts访问

10.0.0.3 linux.wp.com linux.lb.com

#访问
http://linux.wp.com/

6)四层负载均衡配置日志

#四层负载均衡是没有access的日志的,因为在nginx.conf的配置中,access的日志格式是配置在http下的,而四层负载均衡配置是在http以外的;

#如果需要日志则需要配置在stream下面
[root@lb4 ~]# vim /etc/nginx/conf.c/linux.lb4.com.conf
stream {
	log_format  proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
                  '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
    access_log /var/log/nginx/proxy.log proxy;

    upstream lbserver {
        server 10.0.0.4:80;
        server 10.0.0.5:80;
    }

    server {
        listen 80;
        proxy_pass lbserver;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
    }
}

#查看所有web服务器日志
[root@web01 ~]# tail -f /var/log/nginx/access.log
[root@web02 ~]# tail -f /var/log/nginx/access.log

三、四层负载端口转发

1.请求负载均衡的5555端口,跳转到web01的22端口
#简单配置
stream {
	server {
        listen 5555;
        proxy_pass 172.16.1.7:22;
	}
}

#一般配置
stream {
    upstream ssh_7 {
        server 10.0.0.7:22;
    }

    server {
        listen 5555;
        proxy_pass ssh_7;
    }
}
2.请求负载均衡的6666端口,跳转至172.16.1.51:3306
stream {
    upstream db_51 {
        server 172.16.1.51:3306;
    }
    
    server {
        listen 6666;
        proxy_pass db_51;
    }
}
3.数据库从库的负载均衡
tream {
    upstream dbserver {
        server 172.16.1.51:3306;
        server 172.16.1.52:3306;
        server 172.16.1.53:3306;
        server 172.16.1.54:3306;
        server 172.16.1.55:3306;
        server 172.16.1.56:3306;
    }
    
    server {
        listen 5555;
        proxy_pass dbserver;
    }
}

数据库软件
在这里插入图片描述

四、动静分离

动静分离,通过中间件将动态请求和静态请求进行分离;
通过中间件将动态请求和静态请求分离,可以减少不必要的请求消耗,同时能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值