nginx之负载均衡
反向代理模块http_proxy_module
反向代理模块http_proxy_module 七层
Nginx代理支持的协议
- http
- https
- websocket
- tcp/ip
- rtmp
- pop/imap
一、Nginx负载均衡基本概述
当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾

往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB、那么SLB它的调度节点和服务节点通常是在一个地域里面。那么它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。
所以说当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后在转发给调度节点,调度节点最后响应给用户节点。这样也能实现一个均衡的作用,那么Nginx则是一个典型的SLB
负载均衡的叫法有很多:
负载均衡
负载
Load Balance
LB
公有云中叫法
SLB 阿里云负载均衡
QLB 青云负载均衡
CLB 腾讯云负载均衡
ULB ucloud负载均衡
常见的负载均衡的软件
Nginx:在1.9版本之前只能做7层,1.9版本之后,出来一个叫做stream模块,能做4层,7层。
Haproxy:能做4层,7层
LVS:只能做4层,ipvsadm,修改路由,物理服务器,变成一台‘硬件负载均衡’。

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

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

四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发、由此可以看出,七层负载均衡效率没有四负载均衡高。
但七层负载均衡更贴近于服务,如:http协议就是七层协议,我们可以用Nginx可以作会话保持,URL路径规则匹配、head头改写等等,这些是四层负载均衡无法实现的。
注意:四层负载均衡不识别域名,七层负载均衡识别域名
二、Nginx负载均衡配置场景
Nginx要实现负载均衡需要用到proxy_pass代理模块配置.
Nginx负载均衡与Nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池.

Syntax: upstream name { ... }
Default: -
Context: http
#upstream例
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
| 角色 | 外网****IP(NAT) | 内网****IP(LAN) | 主机名 |
|---|---|---|---|
| LB01 | eth0:10.0.0.5 | eth1:172.16.1.5 | lb01 |
| web01 | eth0:10.0.0.7 | eth1:172.16.1.7 | web01 |
| web02 | eth0:10.0.0.8 | eth1:172.16.1.8 | web02 |
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# cat node.conf
server {
listen 80;
server_name node.drz.com;
location / {
root /node;
index index.html;
}
}
[root@web01 conf.d]# mkdir /node
[root@web01 conf.d]# echo "Web01..." > /node/index.html
[root@web01 conf.d]# systemctl restart nginx
[root@web02 ~]# cd /etc/nginx/conf.d/
[root@web02 conf.d]# cat node.conf
server {
listen 80;
server_name node.drz.com;
location / {
root /node;
index index.html;
}
}
[root@web02 conf.d]# mkdir /node
[root@web02 conf.d]# echo "Web02..." > /node/index.html
[root@web02 conf.d]# systemctl restart nginx
[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# cat node_proxy.conf
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name node.drz.com;
location / {
proxy_pass http://node;
include proxy_params;
}
}
[root@lb01 conf.d]# systemctl restart nginx
[root@Nginx ~]# 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 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
打开浏览器访问:http://node.drz.com


如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点down掉的时候,Nginx会更据你具体负载均衡的设置,将请求转移到其他的节点上,但是,如果后台服务连接没有down掉,但是返回错误异常码了如:504、502、500,这个时候你需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,当其中一台返回错误码404,500...等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率。
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
vim upstream.conf
upstream web_7_8 {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name blog.drz.com;
location / {
proxy_pass http://web_7_8;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
include proxy_params;
}
}
三、Nginx负载均衡的调度算法
| 调度算法 | 概述 |
|---|---|
| 轮询(rr) | 按时间顺序逐一分配到不同的后端服务器(默认) |
| weight(wrr) | 加权轮询,weight值越大,分配到的访问几率越高 |
| ip_hash | 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器 |
| url_hash | 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 |
| least_conn | 最少链接数,哪个机器链接数少就分发给哪个机器 |
upstream load_pass {
server 10.0.0.7:80;
server 10.0.0.8:80;
}
upstream load_pass {
server 10.0.0.7:80 weight=5;
server 10.0.0.8:80;
}
具体配置不能和weight一起使用。
#如果客户端都走相同代理, 会导致某一台服务器连接过多
upstream load_pass {
ip_hash;
server 10.0.0.7:80 weight=5;
server 10.0.0.8:80;
}
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
四、Nginx后端的状态
后端Web服务器在前端Nginx负载均衡调度中的状态
| 状态 | 概述 |
|---|---|
| down | 当前的server暂时不参与负载均衡 |
| backup | 预留的备份服务器 |
| max_fails | 允许请求失败的次数 |
| fail_timeout | 经过max_fails失败后, 服务暂停时间 |
| max_conns | 限制最大的接收连接数 |
测试down状态测试该Server不参与负载均衡的调度
#down
upstream web_7_8 {
server 172.16.1.7;
server 172.16.1.8 down;
}
测试backup状态
#backup
upstream web_7_8 {
server 172.16.1.7;
server 172.16.1.8 backup;
}
测试max_fails失败次数和fail_timeout多少时间内失败多少次则标记down
#max_fails
#fail_timeout
upstream web_7_8 {
server 172.16.1.7 max_fails=3 fail_timeout=10s;
server 172.16.1.8 max_fails=3 fail_timeout=10s;
}
测试max_conns最大TCP连接数
upstream web_7_8 {
server 172.16.1.7;
server 172.16.1.8 max_conns=120;
}
五、Nginx负载均衡健康检查
在Nginx官方模块提供的模块中,没有对负载均衡后端节点的健康检查模块,但可以使用第三方模块。
nginx_upstream_check_module来检测后端服务的健康状态。
第三方模块项目地址:TP
1.安装依赖包
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2.下载nginx源码包以及nginx_upstream_check模块第三方模块
[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.解压nginx源码包以及第三方模块
[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
./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
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 web.drz.com;
location / {
proxy_pass http://web;
include proxy_params;
}
location /upstream_check {
check_status;
}
}
1.部署web01
1)web01安装nginx和php
2)创建www用户
[root@web01 php]# groupadd www -g 666
[root@web01 php]# useradd www -u 666 -g 666 -s /sbin/nologin -M
3)修改nginx和PHP启动用户
[root@web01 php]# vim /etc/nginx/nginx.conf
user www;
[root@web01 ~]# sed -i 's#^user.*#user www#g' /etc/nginx/nginx.conf
[root@web01 ~]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf
[root@web01 php]# vim /etc/php-fpm.d/www.conf
user = www
group = www
[root@web01 ~]# sed -i 's#^user =.*#user = www#g' /etc/php-fpm.d/www.conf
[root@web01 ~]# sed -i 's#^group =.*#group = www#g' /etc/php-fpm.d/www.conf
[root@web01 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[root@web01 ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
4)编辑nginx配置文件
[root@web01 php]# vim /etc/nginx/conf.d/blog_proxy.drz.com.conf
server {
listen 80;
server_name blog_proxy.drz.com;
root /code/wordpress;
index index.php index.html;
access_log /var/log/nginx/blog_proxy.drz.com_access.log
main;
location ~\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 php]# vim /etc/nginx/conf.d/zh_proxy.drz.com.conf
server {
listen 80;
server_name zh_proxy.drz.com;
root /code/zh;
index index.php index.html;
access_log /var/log/nginx/zh_proxy.drz.com_access.log main;
location ~\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 php]# nginx -t
5)创建站点目录并授权
[root@web01 php]# mkdir -p /code/{wordpress,zh}
6)代码上线
[root@web01 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz
[root@web01 zh]# unzip WeCenter_3-3-3.zip
7)创建出用户上传目录
[root@web01 php]# chown -R www.www /code/
8)启动nginx和php并加入开机自启
[root@web01 code]# systemctl start nginx php-fpm
[root@web01 code]# systemctl enable nginx php-fpm
2.nfs 远端mysql
9)安装数据库
[root@db01 ~]# yum install -y mariadb-server
10)启动数据库
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable mariadb
11)设置数据库root用户的密码
[root@db01 ~]# mysqladmin -uroot password '123'
12)免交互创建数据库
#创建数据库
[root@db01 ~]# mysqladmin -uroot -p123 create wordpress
[root@db01 ~]# mysql -uroot -p123 -e 'create database zh'
#查看数据库
[root@db01 ~]# mysql -uroot -p123 -e 'show databases'
13)免交互创建程序连接MySQL用户
[root@db01 ~]# mysql -uroot -p123 -e "grant all on wordpress.* to wp@'172.16.1.%' identified by '123'"
[root@db01 ~]# mysql -uroot -p123 -e "grant all on zh.* to zh@'172.16.1.%' identified by '123'"
3.安装博客 知乎
4.部署web02
1)压缩包安装
[root@web02 ~]# rz -E
rz waiting to receive.
[root@web02 ~]# ll
total 20188
-rw-------. 1 root root 1444 Aug 8 06:35 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 473 Aug 1 20:44 host_ip.sh
-rw-r--r-- 1 root root 20663838 Aug 20 11:08 nginx_php.tar.gz
[root@web02 ~]# tar xf nginx_php.tar.gz
[root@web02 ~]# ll
total 20188
-rw-------. 1 root root 1444 Aug 8 06:35 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 473 Aug 1 20:44 host_ip.sh
-rw-r--r-- 1 root root 20663838 Aug 20 11:08 nginx_php.tar.gz
drwxr-xr-x 2 root root 4096 Aug 20 17:12 php
[root@web02 ~]# ll php/
[root@web02 ~]# cd php/
[root@web02 php]# rpm -Uvh *rpm
2)创建www用户
[root@web02 ~]# groupadd www -g 666
[root@web02 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
3)修改nginx启动用户
[root@web02 ~]# vim /etc/nginx/nginx.conf
user www;
[root@web02 ~]# sed -i 's#^user.*#user www#g' /etc/nginx/nginx.conf
[root@web02 ~]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf
4)修改php启动用户
[root@web02 ~]# vim /etc/php-fpm.d/www.conf
user = www
group = www
[root@web02 ~]# sed -i 's#^user =.*#user = www#g' /etc/php-fpm.d/www.conf
[root@web02 ~]# sed -i 's#^group =.*#group = www#g' /etc/php-fpm.d/www.conf
[root@web02 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
[root@web02 ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
5)拷贝web01下code目录至web02
[root@web01 ~]# scp -r /code 172.16.1.8:/
6)授权
[root@web02 ~]# chown -R www.www /code/
7)拷贝web01下配置文件至web02
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# scp ./* 172.16.1.8:/etc/nginx/conf.d/
8)删除default.conf
[root@web02 ~]# ll /etc/nginx/conf.d/
[root@web02 ~]# rm -rf /etc/nginx/conf.d/default.conf
9)启动nginx和php
[root@web02 ~]# systemctl start nginx php-fpm
[root@web02 ~]# systemctl enable nginx php-fpm
10)域名解析==>访问浏览器
5.做一个负载均衡
1)安装nginx
[root@lb01 php]# rpm -ivh nginx-1.16.1-1.el7.ngx.x86_64.rpm
2)创建www用户
[root@lb01 php]# groupadd www -g 666
[root@lb01 php]# useradd www -u 666 -g 666 -s /sbin/nologin -M
3)修改启动用户
[root@lb01 ~]# vim /etc/nginx/nginx.conf
user www;
4)启动nginx
[root@lb01 ~]# systemctl start nginx
5)配置代理
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy.conf
upstream web_7_8 {
server 172.16.1.7;
server 172.16.1.8;
}
server {
listen 80;
server_name up.drz.com;
location / {
proxy_pass http://web_7_8;
proxy_set_header HOST $http_host;
proxy_http_version 1.1;
}
}
[root@lb01 ~]# nginx -t
[root@lb01 ~]# nginx -s reload
6)访问 up.drz.com
6.负载均衡健康检查模块部署
1)安装依赖包
[root@lb01 ~]# yum install -y patch unzip pcre-devel openssl-devel
2)下载nginx源码包和nginx_upstream_check模块第三方模块
#下载nginx源码包
[root@lb01 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
#下载nginx_upstream_check模块
[root@lb01 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@lb01 ~]# rz -E
rz waiting to receive.
3)解压nginx源码包以及第三方模块
[root@lb01 ~]# unzip nginx_upstream_check_module-master.zip
[root@lb01 ~]# tar xf nginx-1.14.2.tar.gz
4)进入nginx目录,打补丁(nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录),编译安装。
[root@lb01 ~]# cd nginx-1.14.2
[root@lb01 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
./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@lb01 nginx-1.14.2]# make && make install
5)检查nginx进程,并关闭,重启nginx
[root@lb01 nginx-1.14.2]# ps -ef|grep nginx
root 7128 1 0 08:15 ? 00:00:00 nginx: master process /usr/sbinnginx -c /etc/nginx/nginx.conf
www 17710 7128 0 16:08 ? 00:00:00 nginx: worker process
root 22683 17625 0 16:29 pts/1 00:00:00 grep --color=auto nginx
[root@lb01 nginx-1.14.2]# kill -9 7128
[root@lb01 nginx-1.14.2]# ps -ef|grep nginx
root 22687 17625 0 16:32 pts/1 00:00:00 grep --color=auto nginx
[root@lb01 nginx-1.14.2]# systemctl start nginx
[root@lb01 nginx-1.14.2]# ps -ef|grep nginx
root 22695 1 0 16:34 ? 00:00:00 nginx: master process /usr/sbinnginx -c /etc/nginx/nginx.conf
www 22696 22695 0 16:34 ? 00:00:00 nginx: worker process
root 22698 17625 0 16:34 pts/1 00:00:00 grep --color=auto nginx
[root@lb01 nginx-1.14.2]# nginx -v
nginx version: nginx/1.14.2
6)在已有的负载均衡上增加健康检查的功能
[root@lb01 conf.d]# vim upstream.conf
upstream web_7_8 {
server 172.16.1.7 backup;
server 172.16.1.8;
server 172.16.1.9;
check interval=5000 rise=1 fall=3 timeout=4000;
}
server {
listen 80;
server_name up.drz.com;
location / {
proxy_pass http://web_7_8;
proxy_set_header HOST $http_host;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
#include proxy_params;
}
location /status {
check_status;
}
}
[root@lb01 conf.d]# nginx -t
[root@lb01 conf.d]# nginx -s reload
7)访问http://up.drz.com/status
# 以上172.0.0.9是不存在的,显示为down
# 修改为以下down不显示
server 172.16.1.7 backup;
server 172.16.1.8 down;
server 172.16.1.8;
server 172.16.1.8;
浙公网安备 33010602011771号