如何在Nginx中动态添加上游?

时间:2017-02-22 05:05:27

标签: nginx lua openresty

我的意思是添加上游,但不是上游服务器

这意味着我没有上游块,如:

upstream backend {
    # ...
}

我想动态创建一个上游块。这就像是:

content_by_lua_block {
    upstream_block.add('backend');
    upstream_block.add_server('backend', '127.0.0.1', 8080);
    upstream_block.add_server('backend', '127.0.0.1', 8081);
    upstream_block.add_server('backend', '127.0.0.1', 8082);
    upstream_block.del_server('backend', '127.0.0.1', 8080);
}

proxy_pass http://backend

3 个答案:

答案 0 :(得分:0)

我发现名为ngx_http_dyups_module的nginx模块符合我的问题。

答案 1 :(得分:0)

我的示例如何基于CPU数量动态添加上游服务器。

服务器。我使用openresty并将其配置为在多个端口上侦听。

worker_processes auto;
error_log logs/openresty.err ;
events {
  worker_connections 1000;
}
http {
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  logs/openresty.log main;


  server {
    listen 127.0.0.1:8080;
    listen 127.0.0.1:8081;
    listen 127.0.0.1:8082;
    listen 127.0.0.1:8083;
    listen 127.0.0.1:8084;
    listen 127.0.0.1:8085;
    listen 127.0.0.1:8086;
    listen 127.0.0.1:8087;
    listen 127.0.0.1:8088;
    listen 127.0.0.1:8089;
    listen 127.0.0.1:8090;
    server_name *.*;
    location / {
      content_by_lua_block {
        --[[ local NumCores = tonumber(os.getenv("NUMBER_OF_PROCESSORS"))
        local NumCores=10
        ]]
        --
        -- local f = io.popen("ps -ef | grep nginx | wc -l ")
        local f = io.popen("/usr/sbin/sysctl  -n hw.ncpu ")
        ngx.print('CPU count: '..f:read())
        f:close()
      }
    }
  }
}

反向代理根据CPU数量动态添加上游服务器。

error_log logs/reverse_openresty.err ;
events {
  worker_connections 1000;
}
http {
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  logs/reverse_openresty.log main;
  upstream backend {
    server 0.0.0.1;   # just an invalid address as a place holder
    balancer_by_lua_block {
      local balancer = require "ngx.balancer"
      local start_port=8080
      local f = io.popen("/usr/sbin/sysctl  -n hw.ncpu ") -- get cpu count
      local cpu_count=tonumber(f:read())
      f:close()
      local max_port=start_port+cpu_count-2
      repeat
        local ok, err = balancer.set_current_peer('127.0.0.1', start_port)
        if not ok then
            ngx.log(ngx.ERR, "failed to set the current peer: ", err)
            return ngx.exit(500)
        end
        start_port=start_port+1
      until start_port>max_port
    }
    keepalive 10;  # connection pool
  }
  server {
    listen 80;
    location / {
        proxy_redirect off;
        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;
        proxy_set_header Host $http_host;
        proxy_pass http://backend; # force using http. as node server.js only have http
      }
  }
}

该配置已在MacO上进行了测试。

答案 2 :(得分:0)

我正在使用ngx_dynamic_upstream。它真的很擅长生产。我从所有者那里分叉了原始文件,并检查了源代码以防万一。