nginx高性能WEB服务器系列之六--nginx负载均衡配置+健康检查

nginx系列友情连接:
nginx高性能WEB服务器系列之一简介及安装
https://www.cnblogs.com/maxtgood/p/9597596.html
nginx高性能WEB服务器系列之二命令管理
https://www.cnblogs.com/maxtgood/p/9597990.html
nginx高性能WEB服务器系列之三版本升级
https://www.cnblogs.com/maxtgood/p/9598113.html
nginx高性能WEB服务器系列之四配置文件详解
https://www.cnblogs.com/maxtgood/p/9598333.html
nginx高性能WEB服务器系列之五--实战项目线上nginx多站点配置
https://www.cnblogs.com/maxtgood/p/9598610.html
nginx高性能WEB服务器系列之六--nginx负载均衡配置+健康检查
https://www.cnblogs.com/maxtgood/p/9599068.html
nginx高性能WEB服务器系列之七--nginx反向代理
https://www.cnblogs.com/maxtgood/p/9599335.html
nginx高性能WEB服务器系列之八--nginx日志分析与切割
https://www.cnblogs.com/maxtgood/p/9599542.html
nginx高性能WEB服务器系列之九--nginx运维故障平常解决方案
https://www.cnblogs.com/maxtgood/p/9599752.htmlhtml

注:原创做品,容许转载,转载时请务必以超连接形式标明文章 原始出处 、做者信息和本声明。不然将追究法律责任。nginx

nginx的强大之处没必要要我细说,当初第一次接触nginx的时候就发现了它的强大之处,而且自我以为很是有必要出一篇记录nginx的各个功能及坑点。web

欢迎你们对nginx感兴趣的朋友们来一块儿学习与及时提出错误及误点。有问题的能够在评论区@我。apache

一:nginx负载均衡配置vim

其实负载均衡的意思很简单明了,网上不少原理一大堆的解释,可能看的似懂非懂。这里本人画了一个简单粗暴的原理图,仅供参考:后端

 

解释:其实nginx 做为一个轻量级、高性能的 web server 主要能够干的就两件事情,服务器

第一件事就是直接做为http server(代替apache,对PHP须要FastCGI处理器支持);
第二件事就是做为反向代理服务器实现负载均衡cookie

由于nginx在处理并发方面的优点,如今这个应用很是常见。
固然了Apache的 mod_proxy和mod_cache结合使用也能够实现对多台app server的反向代理和负载均衡,可是在并发处理方面apache仍是没有 nginx擅长。并发

这里介绍一种实践负载均衡+健康检查的方法。app

直接vim /usr/local/nginx/conf/nginx.conf   修改upstream 段配置文件:

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main 
                      '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

 
    sendfile        on;
 
    keepalive_timeout  65;
    upstream worldcup {
           server 10.124.25.28:8001;
           server 10.124.25.29:8001;
    }

nginx配置文件详解的时候也提到了,注意upstream后面接的关键词,若是须要单台,同端口负载不一样请求的时候,须要制定不一样upstream,如下提供一个实践实例。

实践示例,仅供参考,不作解释:

worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    sendfile        on;
   
    keepalive_timeout  65;

upstream keep_one {
    server 192.168.1.1:8080 weight=1 max_fails=2 fail_timeout=30s;
    server 192.168.1.2:8080 weight=1 max_fails=2 fail_timeout=30s;
}

upstream keep_two {
    server 192.168.1.3:8081 weight=1 max_fails=2 fail_timeout=30s;
    server 192.168.1.4:8081 weight=1 max_fails=2 fail_timeout=30s;
}
server {
        listen       80;
        server_name  localhost;
location / {
            root   html;
            index  index.html index.htm;
        }

        location /one {
        root html;
        index index.html index.htm;
        proxy_pass http://keep_one/;
        proxy_set_header Host $http_host;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For 
       $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        client_max_body_size 300m;
        }

       location /two {
        root html;
        index index.html index.htm;
        proxy_pass http://keep_two/;
        proxy_set_header Host $http_host;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        client_max_body_size 300m;
        }
  }
}

提供两个负载接口,同台服务器,同个IP,同个端口。

 二:nginx负载后端的监控检查

其实以上配置文件中已经配置了健康检查的例子,如下介绍两种健康检查的方式。

方法一:

添加upstream的时候,直接ip+port后接weight=1 max_fails=2 fail_timeout=30s;
###如如下代码
upstream fastdfs_tracker {
    server 192.168.1.1:8080 weight=1 max_fails=2 fail_timeout=30s;
    server 192.168.1.2:8080 weight=1 max_fails=2 fail_timeout=30s;
}
解释:weight为配置的权重,在fail_timeout内检查max_fails次数,失败则剔除均衡。

方法二:

添加upstream的时候,在最后一行添加

###如如下代码:

upstream test{ #负载均衡配置,默认的策略,按时间前后,有其余按ip hash,权重

        server 192.168.1.1:8080;

        server 192.168.1.2:8080;

        server 192.168.1.3:8080;

        check interval=3000 rise=2 fall=3 timeout=3000 type=http port=7070;
}

解释:# interval=3000:间隔3秒检查一次,rise=2:检查2次ok后端节点up,fall=3:三次检查失败后端节点down,timeout=3000:超时时间3秒,type=http:发http检查请求类型,port=8080检查端口,可省略,默认和server 192.168.1.1:8080中的端口一致。

 

 

至此关于nginx最经常使用的负载均衡+健康检查已经配置完成,后系列中还会介绍到相对经常使用的nginx的反向代理。

相关文章
相关标签/搜索