一 环境准备

集群服务器部署
备注:集群中每台服务器的配置保持一样
企业中:
1.先部署好一台LNMP服务器,上传代码信息
2.进行测试访问
3.批量部署多台web服务器
4.将nginx配置文件进行分发
5.将站点目录分发给所有主机
教学中:
1.将web01作为模板机进行克隆
2.修改IP地址和主机名

二 负载均衡服务器部署

1.复制nginx.repo文件到该主机
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
2.安装nginx
yum install -y nginx
3.配置文件
[root@lb01 nginx]# cp nginx.conf{,.bak}
[root@lb01 nginx]# grep -Ev ‘^$|#’ nginx.conf.bak >nginx.conf
ngx_http_upstream_module —-upstream负载均衡 OK
ngx_http_proxy_module —-proxy_pass 反向代理

  1. upstream scxiang{
  2. server 10.0.0.7;
  3. server 10.0.0.8;
  4. server 10.0.0.9;
  5. }
  6. server {
  7. listen 80;
  8. server_name www.scxiang.com;
  9. location / {
  10.   proxy_pass http://scxiang;
  11. }
  12. }

4.实现负载均衡测试
搭建集群测试环境:
for name in www bbs blog;do echo “$name 10.0.0.7”>/html/$name/test.html;done
for name in www bbs blog;do echo “$name 10.0.0.8”>/html/$name/test.html;done
for name in www bbs blog;do echo “$name 10.0.0.9”>/html/$name/test.html;done
修改windows解析文件
10.0.0.5 www.scxiang.com bbs.scxiang.com blog.scxiang.com
负载均衡访问网站异常排错思路:
第一步:负载均衡测试后端web节点服务器是否能够正常访问
[root@lb01 ~]# curl -H host:www.scxiang.com 10.0.0.7/test.html
www 10.0.0.7
[root@lb01 ~]# curl -H host:www.scxiang.com 10.0.0.8/test.html
www 10.0.0.8
[root@lb01 ~]# curl -H host:www.scxiang.com 10.0.0.9/test.html
www 10.0.0.9
第二步:负载均衡,利用curl命令访问负载均衡服务器
产看配置文件
第三步:打开一个xshell连接, ping www.oldboy.com
第四步:配置文件编写不正确

三 负载均衡配置模块详细说明

3.1 ngx_http_upstream_module —-upstream

实现不同调度功能
1.轮询分配请求(平均)
2.权重分配请求(能力越强责任越重)
upstream scxiang{
server 10.0.0.7 weight 10;
server 10.0.0.8 weight 20;
server 10.0.0.9 weight 30;
}
3.实现热备功能(备胎功能)
upstream scxiang{
server 10.0.0.7 ;
server 10.0.0.8 ;
server 10.0.0.9 backup;
}
4.定义最大失败次数 健康检查参数
max_fails=4
5.定义失败之后重发的间隔时间 健康检查参数
fail_timeout=10s 会给失败的而服务器一次机会
实现不同调度算法
1.rr 轮询
2.wrr 加权轮询
3.ip_hash 算法 (出现反复登录的时候)
4.least_conn 根据服务器连接分配资源
upstream scxiang{
wrr/ip_hash/lest_conn;
server 10.0.0.7 ;
server 10.0.0.8 ;
server 10.0.0.9 backup;
}

3.2 ngx_http_proxy_module —-proxy_pass

1.访问不同的网站地址,不能显示不同的网站页面 (面试题)
proxy_set_header Host $host;
2.访问网站用户地址信息无法进行分析统计
proxy_set_header X-Forwarded-For $remote_addr;
3.访问负载均衡会出现错误页面,影响用户体验
proxy_next_upstream error timeout http_404 http_502 http_403;

upstream scxiang{
    server 10.0.0.7;
    server 10.0.0.8;
    server 10.0.0.9;
}
server {
    listen       80;
    server_name  www.scxiang.com;
    location / {
        proxy_pass http://scxiang;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}
server {
    listen       80;
    server_name  bbs.scxiang.com;
    location / {
        proxy_pass http://scxiang;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}
server {
    listen       80;
    server_name  blog.scxiang.com;
    location / {
        proxy_pass http://scxiang;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

四 负载均衡企业实践应用

4.1 根据用户访问的url信息进行负载均衡

第一步: 架构环境规划
/upload 集群-10.0.0.8:80 html/www/upload upload服务器集群
/static 集群-10.0.0.7:80 html/www/static static服务器集群
/ 集群-10.0.0.9:80 html/www
web02上进行环境部署:
[root@web02 ~]# mkdir /html/www/upload
[root@web02 ~]# echo “upload-web集群_10.0.0.8” >/html/www/upload/qytest.html
web01上进行环境部署:
[root@web01 conf.d]# mkdir /html/www/static
[root@web01 conf.d]# echo static-web集群_10.0.0.7 >/html/www/static/qytest.html
web03上进行环境部署:
echo “default-web集群_10.0.0.9” >/html/www/qytest.html

4.2 根据用户的终端类型进行负载均衡

第一步:环境准备
iphone www.scxiang.com —-iPhone_access 10.0.0.7:80 mobile移动段集群
谷歌 www.scxiang.com —-google_access 10.0.0.8:80 web段集群
IE 360 www.scxiang.com —-default_access 10.0.0.9:80 default端集群
web01:
[root@web01 ~]# echo “iphone_access 10.0.0.7” > /html/www/test.html
web02:
[root@web02 ~]# echo “google_access 10.0.0.8” > /html/www/test.html
web03:
[root@web03 ~]# echo “default_access 10.0.0.9” > /html/www/test.html
第二步:编写负载均衡配置文件

upstream mobile{
    server 10.0.0.7;
}
upstream web{
    server 10.0.0.8;
}
upstream default{
    server 10.0.0.9;
}
server {
    listen       80;
    server_name  www.scxiang.com;
    location / {
        if ( $http_user_agent ~* iphone ){
                proxy_pass http://mobile;
        }
        if ( $http_user_agent ~* Chrome ){
                proxy_pass http://web;
        }
        proxy_pass http://default;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_next_upstream error  timeout http_404 http_502 http_403;
    }
}