Nginx七层负载均衡

Nginx负载均衡

1.Nginx负载均衡概述

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

Nginx七层负载均衡_nginx

之前在负载均衡调度器这里采用lvs的比较多,由于lvs只能实现对四层传输层的负载均衡,也就是说只能基于ip和端口号进行转发,假如有很多集群,比如01集群、02集群,端口号都是80,无法进行区分,lvs无法基于这种应用名称来转发进行负载均衡,因此就有了Nginx负载均衡,Nginx负载均衡既可以实现对四层的负载又可以实现对七层的负载,可以根据ip端口也可以根据应用名称来转发。

1.1.负载均衡原理

在之前一直认为负载均衡是当第一个用户连接过来就转发给web01,第二个用户连接过来就转发给web02,其实这是错误的,虽然有2个用户在连接,但是只会产生一个TCP连接,因为负载均衡池是一个整体,负载均衡是根据http请求来转发进行负载的,比如一个页面共有10个http请求,第一个http请求会转发给web01,第二个http请求转发给web02,也就是web01承担1,3,5,7,9的http请求,web02承担2,4,6,8,10的http请求,分工合作,当负载均衡服务器池的节点越多,页面访问的速度就会越快。

2.Nginx常用的云平台介绍

阿里云 SLB

腾讯云 CLB

青云 QLB

ucloud ULB

3.Nginx四层与七层的区别

  • Nginx负载均衡按层划分
  • 二层:基于mac地址
  • 三层:基于ip地址
  • 四层:基于ip和端口号
  • 七层:基于应用协议

四层负载均衡:IP地址、TCP/UDP、端口号

七层负载均衡:HTTP、HTTPS、FTP、SMTP

下图为企业较为常用的负载拓步架构

首先用户发送请求首先会被转发到四层负载均衡上,因为访问网站都是基于域名加端口的,四层就满足了访问条件,然后在根据请求的内容转发到七层负载上,七层负载再根据请求的类型转发到不同的后端web集群,四层和七层也可以在一台服务器上,使用Nginx就可以实现这种架构

Nginx七层负载均衡_mysql_02

4.Nginx负载均衡配置

4.1.Nginx负载均衡配置参数

  • Nginx upstream虚拟配置语法
  • 语法格式:upstream name {…}
  • 默认配置:-
  • 配置区域:http

upstream例子

upstream backend {
server backend1.example.com weight=5;
server backend2.example.com;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}

4.2.web单站点负载均衡配置

4.2.1.环境规划

角色

外网IP

内网IP

主机名

LB01

ens33:192.168.81.210(vm8)

ens37:172.16.1.10(vm1)

localhost

web01

ens33:192.168.81.220(vm8)

ens37:172.16.1.20(vm1)

localhost

web02

ens33:192.168.81.230(vm8)

ens37:172.16.1.30(vm1)

localhost

web03

ens33:192.168.81.240(vm8)

ens37:172.16.1.40(vm1)

localhost

4.2.2.配置web节点服务器

web01配置
[root@localhost ~]# mkdir /web/web01
[root@localhost ~]# echo "web02 172.16.1.20" >/web/web01/index.html
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@www conf.d]# rename .conf .conf.bak *.conf
[root@www conf.d]# vim web01.conf
server {
listen 80;
server_name web.com;
location / {
root /web/web01;
index index.html;
}
}
[root@localhost 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@localhost conf.d]# systemctl reload nginx

web02配置
[root@localhost ~]# mkdir /web/web02
[root@localhost ~]# echo "web02 172.16.1.30" >/web/web02/index.html
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# rename .conf .conf.bak *.conf
[root@localhost conf.d]# vim web02.conf
server {
listen 80;
server_name web.com;
location / {
root /web/web02;
index index.html;
}
}
[root@localhost 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@localhost conf.d]# systemctl reload nginx

web03配置
[root@localhost ~]# mkdir /web/web03
[root@localhost ~]# echo "web03 172.16.1.40" >/web/web03/index.html
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# rename .conf .conf.bak *.conf
[root@localhost conf.d]# vim web03.conf
server {
listen 80;
server_name web.com;
root /web/web03;
index index.html;
}
[root@localhost 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@localhost conf.d]# systemctl reload nginx

4.2.3.配置负载均衡服务器

[root@localhost conf.d]# rename .conf .conf.bak *.conf
[root@localhost conf.d]# vim lb_web.conf
upstream web_lb {
server 172.16.1.20:80;
server 172.16.1.30:80;
server 172.16.1.40:80;
}

server {
listen 80;
server_name web.com;

location / {
proxy_pass http://web_lb;
}
}

重载
[root@localhost 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@localhost conf.d]# systemctl reload nginx

验证效果,会发现每次都会把http请求转发到不同服务器
[root@localhost conf.d]# curl web.com
web01 172.16.1.20
[root@localhost conf.d]# curl web.com
web02 172.16.1.30
[root@localhost conf.d]# curl web.com
web03 172.16.1.40

配置文件讲解
首先用upstream定义一个虚拟服务器池也就是定义一个集群,服务器池的名字可以自己定义,这里定义的名字是web_lb,里面用server指定成员服务器地址,当用户访问http://web.com时(如果使用ip访问,那么server_name就写localhost),会跳转到刚刚定义的虚拟服务器池web_lb

Nginx七层负载均衡_mysql_03

4.3.web多站点负载均衡配置

4.3.1.配置web节点服务器

两个站点:web.com shangmei.com

3个web服务器在上一小节已经配置过一个站点了,现在配置第二个站点,为了验证负载均衡的原理,我们找一个http请求特别多的页面,在对比他们的访问速度,就用尚美的首页

web01配置
1.将网站源码拷贝过来
[root@www ~]# scp -r root@192.168.81.210:/web/shangmei /web/
2.创建站点配置文件
[root@www conf.d]# vim shangmei.conf
server {
listen 80;
server_name shangmei.com;

location / {
root /web/shangmei;
index index.html;
}
}
3.重载
[root@localhost 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@localhost conf.d]# systemctl reload nginx

由于其他节点配置一致,直接scp就可以
小技巧集群配置多机配置一致,可以直接scp,避免出错
在web02、web03上执行,获取网站源代码
[root@localhost conf.d]# scp -r root@192.168.81.210:/web/shangmei /web/

在web01上执行,将配置文件传到其他节点服务器,在重载一下即可
[root@www conf.d]# scp shangmei.conf root@192.168.81.230:/etc/nginx/conf.d/
[root@www conf.d]# scp shangmei.conf root@192.168.81.240:/etc/nginx/conf.d/
[root@www conf.d]# ssh 192.168.81.230 'systemctl reload nginx'
[root@www conf.d]# ssh 192.168.81.240 'systemctl reload nginx'

4.3.2.配置负载均衡服务器

由于web服务器上有多个站点,因此需要配置请求头
upstream web_lb {
server 172.16.1.20:80;
server 172.16.1.30:80;
server 172.16.1.40:80;
}

server {
listen 80;
server_name web.com;

location / {
proxy_pass http://web_lb;
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 128k;
proxy_buffers 4 128k;
}
}

server {
listen 80;
server_name shangmei.com;

location / {
proxy_pass http://web_lb;
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 128k;
proxy_buffers 4 128k;
}
}

重载nginx
[root@localhost 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@localhost conf.d]# systemctl restart nginx


配置完成后遇到点小问题,访问各自域名,居然都请求到一个站点,排查发现web.com的配置文件节点域名写的是web01-03.com与负载均衡配置的web.com对应不上,因此只能访问到尚美

4.3.3.效果

web.com

Nginx七层负载均衡_mysql_04

shangmei.com

Nginx七层负载均衡_mysql_05

配置负载后访问结果

4.4.负载均衡日志配置

获取后端真实服务器地址

1.编辑nginx.conf,修改log_format增加upstream_addr变量,可以获得后端真实服务器ip
[root@localhost conf.d]# vim ../nginx.conf
log_format main '$remote_addr - $http_host - $remote_user [$time_local] $http_host $msec "$request" $request_time '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $upstream_addr';

2.修改负载均衡配置文件,在server中指定日志格式
[root@localhost conf.d]# vim lb_web.conf
server {
access_log /nginx_log/lb_web_com_access.log main;
}

3.重载服务
[root@localhost 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@localhost conf.d]# systemctl reload nginx

4.日志追踪,发现最后一列就是真实服务器ip地址
[root@localhost conf.d]# tail -f /nginx_log/lb_web_com_access.log
192.168.81.1 - web.com - - [19/Apr/2020:20:12:36 +0800] web.com 1587298356.201 "GET / HTTP/1.1" 0.112 200 18 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36" "-" 172.16.1.20:80
192.168.81.1 - web.com - - [19/Apr/2020:20:12:42 +0800] web.com 1587298362.831 "GET / HTTP/1.1" 0.002 200 18 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36" "-" 172.16.1.30:80
192.168.81.1 - web.com - - [19/Apr/2020:20:12:45 +0800] web.com 1587298365.727 "GET / HTTP/1.1" 0.015 200 18 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36" "-" 172.16.1.40:80

5.Nginx负载均衡后端状态

后端web服务器在前端Nginx负载均衡调度中的状态

状态

概述

down

当前的server暂时不参与负载均衡

backup

预留的备份服务器,不只是纯备份,还会跑着其他系统的业务,避免资源浪费

max_fails

允许请求失败的次数,默认值1

fail_timeout

经过max_fails失败后,服务暂停时间,默认值10

max_conns

限制最大的接收连接数

5.1.donw状态

当调度转态配置为down表示这台服务器不在进行负载均衡,退出虚拟服务器池,也就是不参与任何调度,一般用于停机维护

配置非常简单,只需要在服务器地址后面加上调度状态即可
[root@localhost conf.d]# vim lb_web.conf
upstream web_lb {
server 172.16.1.20:80 down;
server 172.16.1.30:80;
server 172.16.1.40:80;
}

[root@localhost 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@localhost conf.d]# systemctl reload nginx

验证效果
172.16.1.20已不再接受负载分配
[root@localhost conf.d]# curl web.com
web02 172.16.1.30
[root@localhost conf.d]# curl web.com
web03 172.16.1.40
[root@localhost conf.d]# curl web.com
web02 172.16.1.30

5.2.backup状态

当负载调度状态配置为backup表示这台服务器为备份状态,当其他服务器异常宕机后,这台服务器会转换成活跃服务器接替坏的服务器提供服务,当活跃服务器恢复后,备份服务器就不在提供服务

将172.16.1.20配置为备份机器
[root@localhost conf.d]# vim lb_web.conf
upstream web_lb {
server 172.16.1.20:80 backup;
server 172.16.1.30:80;
server 172.16.1.40:80;
}

重载
[root@localhost 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@localhost conf.d]# systemctl reload nginx

效果:
也是只有这两台在工作
[root@localhost conf.d]# curl web.com
web02 172.16.1.30
[root@localhost conf.d]# curl web.com
web03 172.16.1.40

当这两台宕机后,172.16.1.20开始工作
[root@localhost conf.d]# curl web.com
web01 172.16.1.20

当172.16.1.30、40服务器恢复后,20将继续承担备份角色不在提高服务

5.3.max_fails状态

max_fails状态表示请求失败的次数,也就是在规定次数内没有请求成功就将此服务器的状态设置为down

将172.16.1.20设置请求失败次数为1,一次失败后就将调度状态设置为down
[roo@localhost conf.d]# vim lb_web.conf
upstream web_lb {
server 172.16.1.20:80 max_fails=1;
server 172.16.1.30:80 down;
server 172.16.1.40:80;
}

重载
[root@localhost 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@localhost conf.d]# systemctl reload nginx

验证效果
将172.16.1.20的nginx停掉
然后跟踪error.log即可看到效果
[root@localhost conf.d]# tail -f /var/log/nginx/error.log
2020/04/20 10:10:45 [error] 22502#22502: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.81.1, server: web.com, request: "GET / HTTP/1.1", upstream: "http://172.16.1.20:80/", host: "web.com"

5.4.fail_timeout状态

fail_timeout状态通常和max_fails一起使用,表示当请求失败后,多少秒后再去重试,类似健康检查

将失败次数设置为3,等待是时间设置为10秒
[root@localhost conf.d]# vim lb_web.conf
upstream web_lb {
server 172.16.1.20:80 max_fails=3 fail_timeout=10s;
server 172.16.1.30:80 down;
server 172.16.1.40:80;
}

重载
[root@localhost 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@localhost conf.d]# systemctl reload nginx

验证效果
可以发现两条日志中间差了10秒
[root@localhost conf.d]# tail -f /var/log/nginx/error.log
2020/04/20 10:34:11 [error] 23235#23235: *157 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.81.1, server: web.com, request: "GET / HTTP/1.1", upstream: "http://172.16.1.20:80/", host: "web.com"
2020/04/20 10:34:22 [error] 23235#23235: *157 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.81.1, server: web.com, request: "GET / HTTP/1.1", upstream: "http://172.16.1.20:80/", host: "web.com"

5.5.max_conns状态

max_conns表示设置最大tcp连接数

[root@localhost conf.d]# vim lb_web.conf 
upstream web_lb {
server 172.16.1.20:80 max_fails=1 fail_timeout=10s;
server 172.16.1.30:80;
server 172.16.1.40:80 max_conns=1;
}

6.Nginx负载均衡算法

Nginx负载均衡调度算法

调度算法

概述

简称

轮询

按时间顺序逐一分配到不同的后端服务器(默认)

不考虑实际负载或实际配置,所有服务器都是平等,平均负载请求

rr

weight

加权轮询,weight值越大,分配到的访问几率越高

wrr

ip_hash

每个请求按访问IP的hash结果分配,这样来自同一IP的客户端固定会访问同一个后端服务器

url_hash

按照访问URL的hash结果来分配请求,每次相同的URL都会定向到同一个后端服务器

least_conn

最少连接数,那个机器连接数少就分发

lc

wlc

加权最小连接

wlc

ip_hash详解

ip_hash有两个点,一个是session一个是cookies,session是基于服务器端,表示会话保持,cookies是基于客户端比如浏览器缓存,当用户访问网站时,负载均衡将请求转发给web01,用户名密码会保存到web01上生成一个session,当转发给web02时有需要重新登录,显然不是我们想要的效果,这时就可以设置ip_hash,让用户始终访问同一台服务器

session最好的办法是开发人员在网站加上一个功能,允许将会话保存在浏览器上,也可以把session通过redis写入数据库里

6.1.轮询具体配置

默认配置

upstream web_lb {
server 172.16.1.20:80;
server 172.16.1.30:80;
server 172.16.1.40:80;
}

6.2.weight加权轮询具体配置

upstream web_lb {
server 172.16.1.20:80 weight=5;
server 172.16.1.30:80;
server 172.16.1.40:80;
}

会看到20的负载最多

Nginx七层负载均衡_mysql_06

6.3.ip_hash具体配置

ip_hash不能与weight一起使用

如果客户端都走相同的代理,会导致某一台服务器连接过多

upstream web_lb {
ip_hash;
server 172.16.1.20:80;
server 172.16.1.30:80;
server 172.16.1.40:80;
}

会看到同一台主机一直都会请求同一个后端服务器

Nginx七层负载均衡_服务器_07

7.Nginx负载均衡综合案例-wp

部署基于LNMP平台的wordpress博客应用并通过负载均衡来实现负载调度,提供访问速度

7.1.环境规划

内部服务器之间统一使用172网段的ip进行通信

角色

外部IP地址

内部IP地址

负载均衡服务器

ens33:192.168.81.210

ens37:172.16.1.10

NFS存储服务器

ens33:192.168.81.210

ens37:172.16.1.10

MySQL服务器

ens33:192.168.81.220

ens37:172.16.1.20

web01

ens33:192.168.81.220

ens37:172.16.1.20

web02

ens33:192.168.81.230

ens37:172.16.1.30

web03

ens33:192.168.81.240

ens37:172.16.1.40

7.2.部署MySQL

1.下载MySQL官方扩展源
[root@localhost ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm

2.安装完mysqlrpm后会在/etc/yum.repos.d中生成两个文件
root@localhost ~]# ls /etc/yum.repos.d/my*
/etc/yum.repos.d/mysql-community.repo /etc/yum.repos.d/mysql-community-source.repo

3.安装mysql5.6
把mysql5.6包下载下来,然后方便下次使用(有条件的可以直接yum装)
[root@localhost ~]# yum install mysql-community-server

4.启动
[root@localhost soft]# systemctl start mysqld
[root@localhost soft]# systemctl enable mysqld

5.登录
[root@localhost ~]# mysql

6.修改密码
[root@localhost ~]# mysqladmin -u root password 123

7.允许其他服务器远程连接mysql
首先在mysql服务器执行:
mysql> grant all privileges on *.* to 'root'@'%' identified by '123' with grant option;
mysql> flush privileges;

8.创建wordpress数据库
mysql> create database wordpress;

远程主机
mysql -uroot -p123 -h 192.168.81.220

7.3.部署NFS

安装nfs
[root@localhost conf.d]# yum -y install nfs-tuils rpcbind

创建NFS存储路径
[root@localhost conf.d]# mkdir /data/wordpress
[root@localhost ~]# chown -R www:www /data/wordpress

[root@localhost conf.d]# vim /etc/exports
/data/wordpress 172.16.1.0/24(rw,sync,no_root_squash)

[root@localhost conf.d]# systemctl restart rpcbind
[root@localhost conf.d]# systemctl restart nfs

[root@localhost conf.d]# showmount -e
Export list for localhost.localdomain:
/data/wordpress 172.16.1.0/24

7.4.部署WordPress网站

三台web服务器都部署WordPress站点,并且数据库都用同一个192.168.81.210

1.将81.220部署好的WordPress拷贝到81.230服务器
[root@localhost conf.d]# scp -r /web/wordpress root@192.168.81.240:/web/
如果没有部署就把源码上传到服务unzip解包即可,然后等配置文件写好,访问前端安装即可

2.编辑站点配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/wordpress.conf
#wordpress
server {
listen 80;
server_name jxl.wordpress.com;
root /web/wordpress;
index index.php index.html;
access_log /nginx_log/jxl_wordpress_access.log main;

location ~ .php$ {
root /web/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

3.编辑WordPress网站配置文件,修改数据库地址为mysql服务器地址
[root@localhost ~]# vim /web/wordpress/wp-config.php
/** MySQL主机 */
define( 'DB_HOST', '192.168.81.220' );


4.重载nginx
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl reload nginx

5.配置81.240节点服务器
直接将230上配置好的站点目录、nginx配置文件拷贝过来启动即可

7.5.挂载NFS

1.测试NFS是否正常
[root@localhost conf.d]# showmount -e 172.16.1.10
Export list for 172.16.1.10:
/data/wordpress 172.16.1.0/24

2.获取WordPress源数据
[root@localhost data]# scp -r root@192.168.81.220:/web/wordpress/wp-content/ /data/wordpress

3.web01配置
[root@localhost conf.d]# mount 172.16.1.10:/data/wordpress /web/wordpress/wp-content/
[root@localhost ~]# chown -R www:www /web/

4.web02配置
[root@localhost conf.d]# mount 172.16.1.10:/data/wordpress /web/wordpress/wp-content/
[root@localhost ~]# chown -R www:www /web/

5.web03配置
[root@localhost conf.d]# mount 172.16.1.10:/data/wordpress /web/wordpress/wp-content/
[root@localhost ~]# chown -R www:www /web/

6.将挂载信息写入/etc/fstab文件中
[root@localhost conf.d]# vim /etc/fstab
172.16.1.10:/data/wordpress /web/wordpress/wp-content nfs defaults 0 0
mount -a 识别

7.写一篇文章上传附件,检查nfs有没有生成文件
[root@localhost conf.d]# ls /data/wordpress/uploads/2020/04/
1寸蓝底-150x150.jpg 1寸蓝底.jpg

7.6.负载均衡配置

[root@localhost conf.d]# vim lb_wordpress.conf
#lb_wordpress
upstream lb_wordpress {
ip_hash; //有登录认证的站点建议使用ip_hash,避免重复认证
server 172.16.1.20:80 max_fails=3 fail_timeout=60;
server 172.16.1.30:80 max_fails=3 fail_timeout=60;
server 172.16.1.40:80 max_fails=3 fail_timeout=60;
}

server {
listen 80;
server_name jxl.wordpress.com;
client_max_body_size 200m;
access_log /nginx_log/lb_jxl_wordpress_access.log main;

location / {
proxy_pass http://lb_wordpress;
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 30;
proxy_buffering on;
proxy_buffer_size 128k;
proxy_buffers 4 128k;
}
}

[root@localhost 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@localhost conf.d]# systemctl reload nginx

8.Nginx负载均衡综合案例-wc

部署基于LNMP的wecenter知乎论坛并通过负载均衡来实现负载调度,提供访问速度

8.1.环境规划

内部服务器之间统一使用172网段的ip进行通信

角色

外部IP地址

内部IP地址

负载均衡服务器

ens33:192.168.81.210

ens37:172.16.1.10

NFS存储服务器

ens33:192.168.81.210

ens37:172.16.1.10

MySQL服务器

ens33:192.168.81.220

ens37:172.16.1.20

web01

ens33:192.168.81.220

ens37:172.16.1.20

web02

ens33:192.168.81.230

ens37:172.16.1.30

web03

ens33:192.168.81.240

ens37:172.16.1.40

8.2.部署MySQL

1.下载MySQL官方扩展源
[root@localhost ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm

2.安装完mysqlrpm后会在/etc/yum.repos.d中生成两个文件
root@localhost ~]# ls /etc/yum.repos.d/my*
/etc/yum.repos.d/mysql-community.repo /etc/yum.repos.d/mysql-community-source.repo

3.安装mysql5.6
把mysql5.6包下载下来,然后方便下次使用(有条件的可以直接yum装)
[root@localhost ~]# yum install mysql-community-server

4.启动
[root@localhost soft]# systemctl start mysqld
[root@localhost soft]# systemctl enable mysqld

5.登录
[root@localhost ~]# mysql

6.修改密码
[root@localhost ~]# mysqladmin -u root password 123

7.允许其他服务器远程连接mysql
首先在mysql服务器执行:
mysql> grant all privileges on *.* to 'root'@'%' identified by '123' with grant option;
mysql> flush privileges;

8.创建discuz数据库
mysql> create database wecenter;

远程主机
mysql -uroot -p123 -h 192.168.81.220

8.3.部署NFS

安装nfs
[root@localhost conf.d]# yum -y install nfs-tuils rpcbind

创建NFS存储路径
[root@localhost conf.d]# mkdir /data/wecenter
[root@localhost ~]# chown -R www:www /data/wecenter

[root@localhost conf.d]# vim /etc/exports
/data/wecenter 172.16.1.0/24(rw,sync,no_root_squash)

[root@localhost conf.d]# systemctl restart rpcbind
[root@localhost conf.d]# systemctl restart nfs

[root@localhost conf.d]# showmount -e
Export list for localhost.localdomain:
/data/wecenter 172.16.1.0/24

8.4.部署Wecenter网站

三台web服务器都部署discuz站点,并且数据库都用同一个192.168.81.210

1.解压源码包
[root@jxl web]# unzip -d wecenter WeCenter_3-3-4.zip

2.赋予权限
[root@jxl web]# chown -R www:www /web/

3.编辑站点配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/wecenter.conf
#wecenter
server {
listen 80;
server_name jxl.wecenter.com;
access_log /nginx_log/jxl_wecenter_access.log main;

location / {
root /web/wecenter;
index index.php index.html;
}

location ~ .php$ {
root /web/wecenter;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

4.重载nginx
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl reload nginx

5.页面安装wecenter
注意要将mysql的地址写成mysql的ip地址

6.配置其他节点服务器
将源码包拷贝至其他服务器
[root@jxl web]# scp -r wecenter/ root@192.168.81.230:/web/
[root@jxl web]# scp -r wecenter/ root@192.168.81.240:/web/
将配置文件拷至其他服务器
[root@jxl web]# scp -r /etc/nginx/conf.d/wecenter.conf root@192.168.81.230:/etc/nginx/conf.d/
[root@jxl web]# scp -r /etc/nginx/conf.d/wecenter.conf root@192.168.81.240:/etc/nginx/conf.d/
在节点服务器操作,赋予权限
[root@jxl web]# chown -R www:www /web/

8.5.挂载NFS

1.测试NFS是否正常
[root@localhost conf.d]# showmount -e
Export list for localhost.localdomain:
/data/wecenter 172.16.1.0/24
/data/wordpress 172.16.1.0/24

2.如果wecenter站点之前存放过附件,那么就需要获取源数据
[root@localhost conf.d]# scp -r root@192.168.81.220:/web/wecenter/uploads/* /data/wecenter/

3.web01配置
[root@localhost ~]# mount 172.16.1.10:/data/wecenter /web/wecenter/uploads/
[root@localhost ~]# chown -R www:www /web/

4.web02配置
[root@localhost ~]# mount 172.16.1.10:/data/wecenter /web/wecenter/uploads/
[root@localhost ~]# chown -R www:www /web/

5.web03配置
[root@localhost ~]# mount 172.16.1.10:/data/wecenter /web/wecenter/uploads/
[root@localhost ~]# chown -R www:www /web/

6.将挂载信息写入/etc/fstab文件中
[root@localhost conf.d]# vim /etc/fstab
172.16.1.10:/data/wecenter /web/wecenter/uploads/ nfs defaults 0 0
使用mount -a即可识别

7.配置完毕后,登录页面写一篇文章,插入图片,在看nfs对应的目录有没有附件上传上来
[root@localhost conf.d]# ls /data/wecenter/article/20200421/
99d1f8039e0b55708b7e0a246377d2cb.jpg

8.6.负载均衡配置

[root@localhost conf.d]# vim lb_wecenter.conf
#lb_wercenter
upstream lb_wecenter {
server 172.16.1.20:80 weight=1 max_fails=3 fail_timeout=60;
server 172.16.1.30:80 weight=1 max_fails=3 fail_timeout=60;
server 172.16.1.40:80 weight=1 max_fails=3 fail_timeout=60;
}

server {
listen 80;
server_name jxl.wecenter.com;
client_max_body_size 200m;
access_log /nginx_log/lb_jxl_wecenter_access.log main;

location / {
proxy_pass http://lb_wecenter;
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 256k;
proxy_buffers 4 256k;
}
}

[root@localhost 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@localhost conf.d]# systemctl reload nginx

9.故障排查

1.检查配置文件是否写错

access_log /nginx_log/lb_jxl_wecenter_access.log main;

location / {
proxy_pass http://lb_wecenter;
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 256k;
proxy_buffers 4 256k;
}

}

[root@localhost 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@localhost conf.d]# systemctl reload nginx

## 9.故障排查

1.检查配置文件是否写错

2.如果访问页面报错404,并且日志中记录的访问路径时其他站点的路径,这时可以修改端口号来解决此问题,有可能是在配置文件夹中有的站点靠前,导致的,或者重启也可以解决

原文链接:https://blog.51cto.com/jiangxl/5076740?b=totalstatistic

原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/5689

(0)
上一篇 2022年10月26日 17:13
下一篇 2022年10月26日 19:02

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

优速盾注册领取大礼包www.cdnb.net
/sitemap.xml