一、负载均衡
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.环境准备
主机 | IP | 身份 |
---|---|---|
lb01 | 10.0.0.4,172.16.1.4 | 负载均衡 |
web01 | 172.16.1.7 | web服务端 |
web02 | 172.16.1.8 | web服务端 |
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 访问
3.web01准备站点
1)配置nginx
[root@web02 ~]# 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@web02 ~]# mkdir /code/lb [root@web02 ~]# echo "我是web0222222222222" > /code/lb/index.html [root@web02 ~]# chown -R www.www /code/
3)配置hosts访问
[root@web02 ~]# systemctl restart nginx 10.0.0.8 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
三、负载均衡调度算法
调度算法 | 概述 |
---|---|
轮询 | 按时间顺序逐一分配到不同的后端服务器(默认) |
weight | 加权轮询,weight值越大,分配到的访问几率越高 |
ip_hash | 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器 |
url_hash | 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 |
least_conn | 最少链接数,哪个机器链接数少就分发 |
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; } #一般用来做会话保持
四、负载均衡后端状态
状态 | 概述 |
---|---|
down | 当前的server暂时不参与负载均衡 |
backup | 预留的备份服务器 |
max_fails | 允许请求失败的次数 |
fail_timeout | 经过max_fails失败后, 服务暂停时间 |
max_conns | 限制最大的接收连接数 |
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.访问错误状态配置
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
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/