用nginx实现负载均衡并添加新模块

nginx负载均衡

 网站的访问量越来越大,服务器的服务模式也得进行相应的升级,比如分离出数据库服务器、分离出图片作为单独服务,这些是简单的数据的负载均衡,将压力分散到不同的机器上。有时候来自web前端的压力,也能让人十分头痛。怎样将同一个域名的访问分散到两台或更多的机器上呢?这其实就是另一种负载均衡了,nginx自身就可以做到,只需要做个简单的配置就行。
  nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态、静态页面的分离,可以按照轮询、ip哈希、URL哈希、权重等多种方式对后端服务器做负载均衡,同时还支持后端服务器的健康检查。

nginx 的 upstream目前支持 4 种方式的分配
1)、轮询(默认)
  每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2)、weight
  指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
3)、ip_hash
  每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
4)、fair(第三方)
  按后端服务器的响应时间来分配请求,响应时间短的优先分配。
5)、url_hash(第三方)

删除server1,重新创建
这里写图片描述
这里写图片描述
这里写图片描述

[root@server1 ~]# tar zxf nginx-1.14.0.tar.gz  #解压nginx包
[root@server1 ~]# ls
[root@server1 ~]# cd nginx-1.14.0
[root@server1 nginx-1.14.0]# ls
[root@server1 nginx-1.14.0]# cd auto/
[root@server1 auto]# cd cc/
[root@server1 cc]# vim gcc

这里写图片描述
这里写图片描述

[root@server1 cc]# yum install -y gcc pcre-devel openssl-devel  
[root@server1 cc]# cd ..
[root@server1 auto]# cd ..
[root@server1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio      #编译第1步
[root@server1 nginx-1.14.0]# make  #编译第2步
[root@server1 nginx-1.14.0]# make install    #编译第3步
[root@server1 nginx-1.14.0]# cd /usr/local/nginx/
[root@server1 nginx]# ls
[root@server1 nginx]# du -sh   #查看大小

这里写图片描述

[root@server1 nginx]# cd sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# pwd
/usr/local/nginx/sbin
[root@server1 sbin]# ln -s /usr/local/nginx/sbin/nginx /sbin  #制作软链接
[root@server1 sbin]# nginx -t   #检测语法
[root@server1 sbin]# nginx   #开启nginx
[root@server1 sbin]# nginx -s stop  #关闭nginx
[root@server1 sbin]# nginx
[root@server1 sbin]# nginx -s reload  #刷新

这里写图片描述

这里写图片描述

[root@server1 sbin]# cd ..
[root@server1 nginx]# ls
[root@server1 nginx]# cd conf
[root@server1 conf]# lscpu   #查看cpu有几个
[root@server1 conf]# vim nginx.conf   #下图

这里写图片描述
这里写图片描述

[root@server1 conf]# useradd -M -d /usr/local/nginx/  nginx
[root@server1 conf]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[root@server1 conf]# nginx -t #检测语法
[root@server1 conf]# nginx -s reload  #刷新
[root@server1 conf]# vim /etc/security/limits.conf 

这里写图片描述

1.轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

[root@server1 conf]# vim nginx.conf

这里写图片描述
这里写图片描述

[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload

开启server1和server2的httpd服务
在物理主机上:

[root@foundation68 ~]# vim /etc/hosts
[root@foundation68 ~]# curl www.westos.org

这里写图片描述
轮询测试
这里写图片描述

2.ip_hash

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
加入ip_hash 原地址不变后端地址也不变,只有这台服务器挂了以后另一台会补上
这里写图片描述

3.weight权重

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
指定轮询机率,weight和访问比率成正比,用于后端服务器性能不均的情况。
这里写图片描述

4.backup

其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
只要在希望成为后备的服务器ip后面多添加一个backup参数,这台服务器就会成为备份服务器。
在平时不使用,nginx不会给它转发任何请求。只有当其他节点全部无法连接的时候,nginx才会启用这个节点。
一旦有可用的节点恢复服务,该节点则不再使用,又进入后备状态。

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
如果将server2和server3的httpd服务关闭,nginx不会给它转发任何请求
这里写图片描述


nginx添加新模块

Sticky工作原理 :
Sticky是nginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上,默认标识名为route
1.客户端首次发起访问请求,nginx接收后,发现请求头没有cookie,则以轮询方式将请求分发给后端服务器。
2.后端服务器处理完请求,将响应数据返回给nginx。
3.此时nginx生成带route的cookie,返回给客户端。route的值与后端服务器对应,可能是明文,也可能是md5、sha1等Hash值
4.客户端接收请求,并保存带route的cookie。
5.当客户端下一次发送请求时,会带上route,nginx根据接收到的cookie中的route值,转发给对应的后端服务器。

[root@server1 ~]# nginx -s stop
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz 
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz 
[root@server1 ~]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng
[root@server1 nginx-1.10.1]#make
[root@server1 nginx-1.10.1]# make install
[root@server1 nginx-1.10.1]# cd conf
[root@server1 conf]# ls
[root@server1 conf]# cd /opt/nginx/
[root@server1 nginx]# cd conf/
[root@server1 conf]# ls
[root@server1 conf]# cp /usr/local/nginx/conf/nginx.conf .
cp: overwrite `./nginx.conf'? y
[root@server1 conf]# vim nginx.conf

这里写图片描述

[root@server1 conf]# /opt/nginx/sbin/nginx  -t   #检测语句
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
[root@server1 conf]# /opt/nginx/sbin/nginx   #开启nginx

将server2和server3的httpd开启
这里写图片描述
每台后端真实服务器都会有一个唯一的route值,所以不管你真实服务器前端有几个装了sticky的nginx代理,它都是不会变化的. 这个cookie是会话方式的,所以你浏览器关闭了,服务器会给你重新分配一台服务器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值