Http 如何在nginx中创建自定义位置?

Http 如何在nginx中创建自定义位置?,http,nginx,lua,Http,Nginx,Lua,我有一个应用程序运行在nginx后面。为了允许客户在同一主机名下添加他们自己的工具,我们使用类似于以下内容的位置: location /some-extension { rewrite ^/(.*) /$1 break; proxy_pass http://customers-app/$1/$args; } 现在,我想让这个动态化,以便给定的客户可以创建零个或更多这样的位置。由于应用程序是使用Docker部署的,因此无法手动编辑nginx配置 Nginx是使用perl和lua支持编译的

我有一个应用程序运行在nginx后面。为了允许客户在同一主机名下添加他们自己的工具,我们使用类似于以下内容的
位置

location /some-extension {
  rewrite ^/(.*) /$1 break;
  proxy_pass http://customers-app/$1/$args;
}
现在,我想让这个动态化,以便给定的客户可以创建零个或更多这样的位置。由于应用程序是使用Docker部署的,因此无法手动编辑nginx配置

Nginx是使用perl和lua支持编译的,所以我考虑了如下内容:

  • 在表单
    path1 url1 path-2 url2上使用环境变量。。。pathn urln
    配置外部工具
  • 在特殊的
    位置
    配置中,将请求URL的第一个路径段与环境变量相匹配,并将
    proxy\u pass
    与相应的URL(如果找到)相匹配
到目前为止,我的情况如下:

location / {
    content_by_lua '
      local target_path = ngx.var.uri;
      local name = nil

      if target_path:len() > 0 then
        startAt = target_path:find("/") + 1
        endAt = target_path:find("/", startAt) - 1
        name = target_path:sub(startAt,endAt)
        ngx.say(name)
      end

      if name then
        local custom_proxies = os.getenv("CUSTOM_PROXIES");
        inString = custom_proxies:find("another ")
        if not inString then
          ngx.say("not in string")
        else
          startAt = custom_proxies:find(" ", inString + 1) + 1
          endAt = custom_proxies:find(" ", startAt)

          url = custom_proxies:sub(startAt,endAt)
          ngx.say(url)

        end
      end

    ';
  }

我知道我不应该使用
content\u by\u lua
,但这似乎是一种工作。问题是,我如何才能将此代理传递到指定的URL?

我通过以下方式解决了此问题:

  • 环境变量应该是一个以空格分隔的名称和URL列表<代码>键http://example.com/app1 键2http://acme.com

  • Lua脚本将环境变量读入
    ,其中
    为键,以下URL为值

  • nginx
    location
    块将处理请求,并使用Lua脚本查找要代理的模块

  • 函数将接受请求路径并从中找到键,然后在表中查找URL

以下是脚本:

local custom_proxies = os.getenv("CUSTOM_PROXIES")
local custom_modules = {}

local cutFrom = 0
local cutTo = 0

while cutTo < custom_proxies:len() do
  cutFrom = cutTo
  cutTo = custom_proxies:find(" ", cutFrom + 1)
  local name = custom_proxies:sub(cutFrom, cutTo - 1)
  cutFrom = cutTo + 1
  cutTo = custom_proxies:find(" ", cutFrom)

  if not cutTo then
    cutTo = custom_proxies:len() + 1
  end

  local url = custom_proxies:sub(cutFrom, cutTo - 1)
  custom_modules[name] = url
  cutTo = cutTo + 1
end

function custom_modules.get_url(target_path)
  local name = nil

  local startAt = target_path:find("/", 6) + 1
  local endAt = target_path:find("/", startAt)
  if not endAt then
    endAt = target_path:len()
  else
    endAt = endAt - 1
  end
  name = target_path:sub(startAt,endAt)
  if name then
    return custom_modules[name]
  else
    ngx.log(ngx.STDERR, "Not able to deduct module name from URI")
    return ""
  end
end

return custom_modules

其思想是,像
/custom/foobar/path/to/something?page=2
这样的请求将使用
foobar
作为一个键来查找URL,并将
proxy\u与路径和参数的其余部分一起传递给它。

我用以下方法解决了它:

  • 环境变量应该是一个以空格分隔的名称和URL列表<代码>键http://example.com/app1 键2http://acme.com

  • Lua脚本将环境变量读入
    ,其中
    为键,以下URL为值

  • nginx
    location
    块将处理请求,并使用Lua脚本查找要代理的模块

  • 函数将接受请求路径并从中找到键,然后在表中查找URL

  • 以下是脚本:

    local custom_proxies = os.getenv("CUSTOM_PROXIES")
    local custom_modules = {}
    
    local cutFrom = 0
    local cutTo = 0
    
    while cutTo < custom_proxies:len() do
      cutFrom = cutTo
      cutTo = custom_proxies:find(" ", cutFrom + 1)
      local name = custom_proxies:sub(cutFrom, cutTo - 1)
      cutFrom = cutTo + 1
      cutTo = custom_proxies:find(" ", cutFrom)
    
      if not cutTo then
        cutTo = custom_proxies:len() + 1
      end
    
      local url = custom_proxies:sub(cutFrom, cutTo - 1)
      custom_modules[name] = url
      cutTo = cutTo + 1
    end
    
    function custom_modules.get_url(target_path)
      local name = nil
    
      local startAt = target_path:find("/", 6) + 1
      local endAt = target_path:find("/", startAt)
      if not endAt then
        endAt = target_path:len()
      else
        endAt = endAt - 1
      end
      name = target_path:sub(startAt,endAt)
      if name then
        return custom_modules[name]
      else
        ngx.log(ngx.STDERR, "Not able to deduct module name from URI")
        return ""
      end
    end
    
    return custom_modules
    
    其思想是,像
    /custom/foobar/path/to/something?page=2
    这样的请求将使用
    foobar
    作为一个键来查找URL,并将
    proxy\u与路径和参数的其余部分一起传递给它