Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Nginx中动态添加上游?_Nginx_Lua_Openresty - Fatal编程技术网

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

如何在Nginx中动态添加上游?,nginx,lua,openresty,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('backe

我的意思是在上游添加一个服务器,但在上游添加一个服务器

这意味着我没有像这样的上游区块:

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

我发现一个名为matches my question的nginx模块。

您可以使用balancer\u by\u lua*和

您将完全控制为给定请求选择的上游


您可以使用关于如何基于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
      }
  }
}
服务器。我使用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
      }
  }
}

配置在MacOs上进行了测试。

我正在使用。它真的很擅长生产。为了以防万一,我从所有者那个里翻出了原稿并检查了源代码。

我想不出有什么原因。只需将变量和proxypass设置为it@AlexeyTen我有很多机器组,每个组都有自己的机器。它们都是动态的。+1,我知道这并不能回答问题,但这是第一个使用
balancer\u by\u lua\u block
的完整运行示例。