Nginx 负载均衡配置

一  、 Nginx反向代理知识概念说明:
      集群概念介绍说明:    一堆干相同事情的服务器,称为集群
      集群概念的特点说明:高可用  高性能 (核心特点说明) 
      集群的分类说明:      负载均衡集群  高可用集群
      负载均衡集群概念说明: 前面是负载均衡器,后面是集群的节点
      负载均衡集群:  会出现雪崩效应
      高可用集群:     利用keepalived实现,一个机器宕机了,另一个机器可以顶替上
      反向代理实现软件为:  nginx(7层http https )层次进行回顾---Nginx支持7层,1.9以后也支持4层了
      LVS支持4层, haproxy(4层和7层 属于反向代理软件)
      硬件负载均衡设备说明:
      硬件和软件使用场景(企业中软硬件选型),中小型场景;
      门户网站 (LVS Tnginx)大型企业网站;
      

1.1  、负载均衡的作用  ;

① 、转发功能

        按照一定的算法【权重、轮询】,将客户端请求转发到不同应用服务器上,减轻单个服务器压力,提高系统并发量。

② 、故障移除

        通过心跳检测的方式,判断应用服务器当前是否可以正常工作,如果服务器期宕掉,自动将请求发送到其他应用服务器。

③ 、恢复添加

       如检测到发生故障的应用服务器恢复工作,自动将其添加到处理用户请求队伍中。

 

1.2 、nginx 负载均衡三种策略 ;

 ① 、轮循(默认) 
            Nginx根据请求次数,将每个请求均匀分配到每台服务器

 ② 、最少连接 
         将请求分配给连接数最少的服务器。Nginx会统计哪些服务器的连接数最少。

 ③ 、IP Hash 
       绑定处理请求的服务器。第一次请求时,根据该客户端的IP算出一个HASH值,将请求分配到集群中的某一台服务器上。后面该客户端的           所有请求,都将通过HASH算法,找到之前处理这台客户端请求的服务器,然后将请求交给它来处理。

 

二 、 Nginx  负载均衡部署 ;  ( lb01 web01 web02  操作 )

[root@lb01 ~]# yum install -y pcre-devel openssl-devel                
mkdir -p /server/tools
cd /server/tools
wget -q http://nginx.org/download/nginx-1.10.3.tar.gz
ls -l nginx-1.10.3.tar.gz
useradd nginx -s /sbin/nologin -M
tar xf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure  --user=nginx --group=nginx --prefix=/application/nginx-1.10.3 --with-http_stub_status_module  --with-http_ssl_module
make
make install
ln -s /application/nginx-1.10.3 /application/nginx

 

# 安装部署完成进行检查
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx
curl localhost

输入  IP 地址 可以访问到  nginx 的欢迎界面  ;

 

三 、 统一编 写nginx配置( 2台 web服务器端操作 )
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
      access_log  logs/access_bbs.log  main;
    }        
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        access_log  logs/access_www.log  main;
    }
}

3.1 、统一nginx测试环境(2台 web服务器端操作 )
mkdir -p /application/nginx/html/{www,bbs}
for name in www bbs; do echo $name `hostname` >/application/nginx/html/$name/index.html;done
for name in www bbs; do cat /application/nginx/html/$name/index.html;done

 

注意哦 !  配置完成后记得检查语法和 reload nginx  ;
/application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
/application/nginx/sbin/nginx -s reload

说明:在浏览器测试前,重启或启动nginx服务程序( web01 web02)
            
 ①. 浏览器测试web服务(利用IP地址进行访问)

 

 ②. nginx 常用模块 ;
            upstream模块: 类似于一个池塘,将nginx节点防止到池塘中
            proxy模块:     用池塘里面的nginx节点,利用proxy进行调用


ngx_http_ssl_module
ngx_http_log_module
ngx_http_status_module
ngx_http_upstream_module
ngx_http_proxy_module           

 

四 、 配置文件编写内容  (lb01 这台设备操作 )

[root@lb01 conf]# egrep -v "^$|#" /application/nginx/conf/nginx.conf.default >nginx.conf

[root@lb01 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream server_pools {           --->>  通过  upstream 这个命令创建了  server_pools  可以说是个组                               
        server 10.0.0.7:80;                               (--->>  表示这个组里有两台服务器,       
        server 10.0.0.8:80;                    
    } 


    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = / {                --->>   这里的  " / "  表示默认的,不管访问的是什么都放在后边的那个组中 ;
        proxy_pass http://server_pools;      --->>  根据用户请求负责扔到池塘中 ,池塘的名字 server_pools
        }
    }
}

           

4.1 、 负载均衡配置完毕后,进行负载测试,刷新页面,可以使用 curl 

 

4.2 、 增加负载均衡参数介绍-深入参数说明
 lb01 nginx.conf   multi hosts 
          upstream server_pools {
            server 10.0.0.7 weight=1 max_fails=3 fail_timeout=10s;     
            server 10.0.0.8 weight=1 max_fails=3 fail_timeout=10s;
               }

 --->>  weight   表示权重,就是安排哪台服务器多干点活,或者是平均干活  ;

---->> max_fails=3   表示最大的失败的次数 ,一般默认 设置为 3 即可  失败了 3 次之后就用下台设备 ;

--->> fail_timeout=10s      表示 过10 秒钟 在连接一下这台设备 看看设备是否已可以使用  ;

 

Nginx 负载均衡 , ip_hash  算法解释  ;  ip_hash 缺点: 负载不均,服务器断了用户的东西不保 ,

                                                                       解决办法:  可以把这些信息都放到 memcached 或者 Redis 

 

五  、 cookie和session的区别 ( 都是用来存放用户的登陆信息 )
      1.都可以存放用户的信息
      
      2.cookie 都是存放在在客户端浏览器中
      1)变量名字和变量的内容
      2)开发人员制定
      3)每个网站(域名)对应着 cookie是相同 相同的网站相同的 cookie 如;一把钥匙开一把锁 
      4)钥匙(字符串 文本)
      
      3.session 存放在服务器
      1)用户密码 登录信息(登录) ----打包  加密---锁头 
      2)不用重新登录
         

 

**************************   附录   **************************

反向代理 与 负载均衡 的区别  ;

lvs  这里只负责转发,不负责处理 ,

nginx 可以在7层也可以在4层   |  4层主要是负责处理端口的   , 7层主要是负责处理网址 

 

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值