nginx负载均衡

keepalived

1.轮循 每个请求逐个分发到后端服务器

2.加权轮循 按照分配的权重将请求分发到后端服务器

3.ip hash 轮询的基础上,保持一个客户端多次请求分发到一台后端服务器上

4.热备, 当挂了,才启用 backup
5.fair法(非官方)

6.sina consoul

代理服务器

正向代理代理客户端,反向代理代理服务器。

nginx-upsync-module

##动态去consul 获取注册的真实反向代理地址

   地址: https://github.com/weibocom/nginx-upsync-module#upstream_show

upstream abing{
 # fake server otherwise ngx_http_upstream will report error when startup
  server 127.0.0.1:11111;
 upsync192.168.212.134:8500/v1/kv/upstreams/itmayieduupsync_timeout=6mupsync_interval=500ms upsync_type=consul strong_dependency=off;
 upsync_dump_path  /usr/local/nginx/conf/servers/servers_test.conf;
}
server {
listen       80;
server_name  localhost;
   location / {
  proxy_passhttp://abing;
 proxy_connect_timeout 1s;   ###nginx发送给上游服务器(真实访问的服务器)超时时间
 proxy_send_timeout 1s;      ###nginx接受上游服务器(真实访问的服务器)超时时间
 proxy_read_timeout 1s;
 index  index.html index.htm;
 }
}

balancer_by_lua

upstream backend{
    server 0.0.0.0;
    balancer_by_lua_block {
        local balancer = require "ngx.balancer"
        local host = {"192.168.1.111", "192.168.1.112"}
        local backend = ""
        local port = ngx.var.server_port
        local remote_ip = ngx.var.remote_addr
        local key = remote_ip..port
        local hash = ngx.crc32_long(key);
        hash = (hash % 2) + 1
        backend = host[hash]
        ngx.log(ngx.DEBUG, "ip_hash=", ngx.var.remote_addr, " hash=", hash, " up=", backend, ":", port)
        local ok, err = balancer.set_current_peer(backend, port)
        if not ok then
            ngx.log(ngx.ERR, "failed to set the current peer: ", err)
            return ngx.exit(500)
        end
        ngx.log(ngx.DEBUG, "current peer ", backend, ":", port)
    }
}

  

ngx_http_dyups_module

原文地址:https://www.cnblogs.com/yizhou35/p/12151806.html