Nginx一般作为反向代理服务器来实现反向代理来转发处理请求,同时也可以作为静态资源服务器来加快静态资源的获取和处理。

1.正向代理与反向代理

正向代理:

正向代理 是一个位于客户端和原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端必须要进行一些特别的设置才能使用正向代理。

作用:访问原来无法访问的资源;可以做缓存,加速访问资源;对客户端访问授权,上网进行认证;代理可以记录用户访问记录,对外隐藏用户信息

反向代理:

反向代理是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。客户端是无感知代理的存在的,反向代理对外都是透明的,访问者者并不知道自己访问的是一个代理。因为客户端不需要任何配置就可以访问。

作用:保证内网的安全,可以使用反向代理提供WAF功能,阻止web攻击;负载均衡,通过反向代理服务器来优化网站的负载

2.Nginx架构

如图所示:Nginx采用多进程方式,一个Master与多个Worker进程。客户端访问请求通过负载均衡配置来转发到不同的后端服务器依次来实现负载均衡。

3.Nginx负载均衡策略

Nginx负载均衡策略主要有 轮询,加权轮询,最少连接数以及IP Hash。

负载均衡配置文件如下:

轮询策略: 实现请求的按顺序转发,即从服务srv1--srv2--srv3依次来处理请求

When the load balancing method is not specifically configured, it defaults to round-robin. All requests are proxied to the server group myapp1, and nginx applies HTTP load balancing to distribute the requests.

加权轮询策略: 请求将按照服务器的设置权重来实现请求转发和处理,如下所示,最终请求处理数将为3:1:1

With the round-robin in particular it also means a more or less equal distribution of requests across the servers — provided there are enough requests, and when the requests are processed in a uniform manner and completed fast enough.

最少连接数策略:  请求将转发到连接数较少的服务器上

With the least-connected load balancing, nginx will try not to overload a busy application server with excessive requests, distributing the new requests to a less busy server instead.

Ip Hash策略:  web服务需要共享session,使用该策略可以实现某一客户端的请求固定转发至某一服务器

If there is the need to tie a client to a particular application server — in other words, make the client’s session “sticky” or “persistent” in terms of always trying to select a particular server — the ip-hash load balancing mechanism can be used.

With ip-hash, the client’s IP address is used as a hashing key to determine what server in a server group should be selected for the client’s requests. This method ensures that the requests from the same client will always be directed to the same server except when this server is unavailable.

由以上可知,轮询只是简单实现请求的顺序转发,并没有考虑不同服务器的性能差异;加权轮询设置了初始时服务器的权重,但是没有考虑运行过程中的服务器状态;IP Hash保证同一个客户端请求转发到同一个后台服务器实现了session保存,然而当某一后台服务器发生故障时,某些客户端将访问失败;最少连接数只是考虑了后端服务器的连接数情况,并没有完全考虑服务器的整体性能。

4.动态负载均衡

动态负载均衡策略类似于加权轮询策略,可以通过对于后端服务器集群的状态监测,量化不同服务器的性能差异,来周期性调整服务器的比重来实现权重的动态调整。

主要待解决的问题:

1.后端服务器集群的性能量化计算来分配不同权重。这方面有很多研究,以后慢慢学习,主要是从后端服务器的cpu,内存,io,网络以及连接数等方面来量化不同的服务器性能。

2.如何实现权重的动态调整,之前一直想写一个nginx第三方模块来实现,但是难度太大。

这里搜集了一些资料,可以通过consul+nginx-upsync-module模块来实现权重的动态调整

https://github.com/weibocom/nginx-upsync-module

利用consul模块来实现权重服务的配置信息

nginx周期性从consul中读取配置信息来动态改变权重

5.consul+nginx-upsync-module实现动态负载均衡

consul安装

下载consul:https://releases.hashicorp.com/consul

关闭防火墙:

启动consul:    ./consul agent -advertise=10.112.99.152 -client=0.0.0.0 -dev 

访问图形化界面:  http://10.112.99.152:8500

nginx安装

http://nginx.org/download/

解压:tar -zxvf nginx-1.9.10.tar.gz

配置环境;

nginx-upsync-module安装  

https://github.com/weibocom/nginx-upsync-module

解压该模块:

unzip nginx-upsync-module-master.zip

将nginx-upsync-module编译到nginx中

cd nginx-1.9.10

安装第三方库

yum -y install openssl openssl-devel

注意下面安装路径应与上面路径对应

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --http-client-body-temp-path=/data/dynamic/nginx/client/ --http-proxy-temp-path=/data/dynamic/nginx/proxy/ --http-fastcgi-temp-path=/data/dynamic/nginx/fcgi/ --http-uwsgi-temp-path=/data/dynamic/nginx/uwsgi --http-scgi-temp-path=/data/dynamic/nginx/scgi --with-pcre --add-module=../nginx-upsync-module-master

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --http-client-body-temp-path=/data/dynamic/nginx/client/ --http-proxy-temp-path=/data/dynamic/nginx/proxy/ --http-fastcgi-temp-path=/data/dynamic/nginx/fcgi/ --http-uwsgi-temp-path=/data/dynamic/nginx/uwsgi --http-scgi-temp-path=/data/dynamic/nginx/scgi --with-pcre --add-module=../nginx-upsync-module-master

make && make install  编译成功

动态负载均衡配置

nginx配置文件:

 
  1. upstream test {

  2. server 127.0.0.1:11111;

  3. upsync 127.0.0.1:8500/v1/kv/upstreams/test/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;

  4. upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;

  5. include /usr/local/nginx/conf/servers/servers_test.conf;

  6. }

  7. server {

  8. listen 80;

  9. server_name localhost;

  10. #charset koi8-r;

  11. #access_log logs/host.access.log main;

  12. location = / {

  13. proxy_pass http://test;

  14. }

  15. error_page 500 502 503 504 /50x.html;

  16. location = /50x.html {

  17. root html;

  18. }

  19. }

启动nginx服务,如果报错

则添加nginx用户

useradd nginx -s /sbin/nologin -M

再次启动

/usr/local/nginx/sbin/nginx

在服务器的8088,8089端口启动两个tomcat

向consul中写入后端服务器信息:

curl -X PUT http://10.112.99.152:8500/v1/kv/upstreams/test/10.112.99.152:8088

curl -X PUT http://10.112.99.152:8500/v1/kv/upstreams/test/10.112.99.152:8089

观察配置文件:

cat /usr/local/nginx/conf/servers/servers_test.conf

此时访问ip实习负载均衡,可以通过改变consul中数据来实现服务器数量以及权重的改变

其余consul中参数配置命令:

upsync参数:  配置consul   key  value  后端服务器ip  port 权重

syntax: upsync $consul/etcd.api.com:$port/v1/kv/upstreams/$upstream_name/ [upsync_type=consul/etcd] [upsync_interval=second/minutes] [upsync_timeout=second/minutes] [strong_dependency=off/on]

The parameters' meanings are:

  • upsync_interval

    pulling servers from consul/etcd interval time.

  • upsync_timeout

    pulling servers from consul/etcd request timeout.

  • upsync_type

    pulling servers from conf server type.

  • strong_dependency

    when strong_dependency is on, nginx will pull servers from consul/etcd every time when nginx start up or reload.

upsync_dump_path参数: 保存consul配置信息

syntax: upsync_dump_path $path

default: /tmp/servers_$host.conf

context: upstream

description: dump the upstream backends to the $path.

利用http命令向consul中写入后端服务器以及权重信息:

增加服务器指令:  默认权重  以及添加权重

curl -X PUT http://$consul_ip:$port/v1/kv/upstreams/$upstream_name/$backend_ip:$backend_port
default: weight=1 max_fails=2 fail_timeout=10 down=0 backup=0;
 
  1. curl -X PUT -d "{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port

  2. or

  3. curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend

删除服务器:

curl -X DELETE http://$consul_ip:$port/v1/kv/upstreams/$upstream_name/$backend_ip:$backend_port

调整服务器的权重:

 
  1. curl -X PUT -d "{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10}" http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port

  2. or

  3. curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port

标记服务器停止服务:

 
  1. curl -X PUT -d "{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10, \"down\":1}" http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port

  2. or

  3. curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":1}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port

检查服务器状态:

curl http://$consul_ip:$port/v1/kv/upstreams/$upstream_name?recurse

nginx管理命令如下:

nginx文件目录: /usr/local/nginx/

配置文件: /usr/local/nginx/conf/nginx.conf

服务器配置信息:/usr/local/nginx/conf/servers/servers_test.conf

nginx操作指令:

参考地址:

https://www.cnblogs.com/Anker/p/6056540.html

https://www.cnblogs.com/dormant/p/5218266.html

http://nginx.org

https://github.com/weibocom/nginx-upsync-module

https://www.aliyun.com/jiaocheng/145775.html

负载均衡和动态负载均衡相关推荐

  1. 静态负载均衡和动态负载均衡_动态负载平衡

    静态负载均衡和动态负载均衡 动态负载平衡 (Dynamic Load Balancing) The algorithm monitors changes on the system workload ...

  2. 网络动态负载均衡算法分析

    转自CSDN博客:http://blog.csdn.net/wallacexiang/archive/2009/07/24/4376147.aspx 随着Internet的日益普及,无论在企业网.园区 ...

  3. 华为无线设备配置动态负载均衡

    配置LSW和AC,使AP与AC之间能够传输CAPWAP报文 [LSW1]vlan batch 10 [LSW1-GigabitEthernet0/0/1]port link-type trunk [L ...

  4. 负载均衡的动态算法和静态算法

    一.前言 相信大家对于负载均衡是做什么的很了解了吧,本篇将带大家剖析其中的动态算法和静态算法的含义以及具体的算法. 二.动.静态算法分别的含义 现有的负载均衡算法主要分为静态和动态两类.静态负载均衡算 ...

  5. 如何让 Nginx 动态负载均衡

    一.什么是动态负载均衡 传统的 Nginx 负载均衡,是在 Nginx config 配置文件中,对 upstream 进行修改(扩展新服务器)进行,进行负载均衡,此时就需要让 Nginx 重启加载 ...

  6. openresty lua-resty-balancer动态负载均衡

    openresty lua-resty-balancer动态负载均衡 lua-resty-balancer:https://github.com/openresty/lua-resty-balance ...

  7. Kong动态负载均衡与服务发现

    Kong动态负载均衡 一.背景 二.通过docker 安装 Kong 三.分布式API网关存在的意义 四.Kong 的相关特性 五.Kong 体系结构 六.Kong 工作流程 七.从 nginx 配置 ...

  8. mqtt服务器性能分析,MQTT服务器动态负载均衡的研究与应用

    1. 引言 随着越来越多的工业设备接入物联网,因处理器能力.网络带宽等具有局限性,所以对其通信技术提出了更高的要求 [1].作为网页标准的HTTP,已不能满足机器之间的大规模沟通,其请求/回答模式不再 ...

  9. Nginx动态负载均衡与配置管理

    背景 在Nginx集群有一定的规模时,比较让人头疼的问题有2个,一是如何在不reload nginx的情况下,动态更新后端rs,减少nginx reload的性能损耗,也能更好的对接到内部的部署平台: ...

最新文章

  1. [C] 图的广度优先遍历
  2. 谈谈中小创业型网站防DDOS及CC,我的草根站长经历。
  3. 细数sass安装中遇到的坑
  4. windows延缓写入失败相关问题解决办法
  5. floquet端口必须沿z轴设置_Ansys Workbench 振动给料机偏心轴的模态分析
  6. python 打开网页获取cookies_python 携带cookie获取页面内容
  7. Action详解(一)
  8. Apache中配置ASP.NET环境
  9. 大工18秋计算机文化基础在线测试2,大工18秋《计算机文化基础》在线测试2
  10. Mac端Java开发分析工具JProfiler 13.0.1
  11. 配置Spring的用于解决懒加载问题的过滤器
  12. 记飞机大战小游戏1.0
  13. 蔻享学术下载器:KouShare-dl
  14. 上海财经应用统计考python_20上财应用统计415分经验帖(初试第一)
  15. 深度学习:卷积神经网络从入门到精通
  16. 《犹太人想的和你不一样》
  17. Java教练和运动员,乒乓球和篮球出国打比赛,需要学英语
  18. 微信小程序实现vtt视频字幕
  19. 《万人如海一身藏》书摘
  20. java工厂方法_Java设计模式之工厂方法模式

热门文章

  1. icon图标素材免费搜索下载网站分享
  2. Java基础学习01(JDK)
  3. 12个Java长久占居主要地位的原因
  4. java 合并pdf报错,[Java教程]java合并PDF文件
  5. FFmpeg入门详解之63:画龙点睛:捋起袖子亲手操练直播项目
  6. CC110L 与CC1101的区别
  7. MIT6.824(lab2A-领导人选举)
  8. 给打算创建一个欢乐的婚礼建议
  9. 区分A~E类IP地址
  10. 计算机网络(谢希仁第8版)第一章课后习题