Search code examples
nginxluaopenresty

Two upstreams, one http, the other https


Good day to all! I have one problem when setting up openresty, please help. What I have: openresty + lua. Openresty config:

upstream a {
   server 1.1.8.1:4707;
}

upstream c {
   server 1.1.8.1:4707;
}
server {
location / {
   proxy_set_header X-Forwarded-Host $host;
   proxy_set_header X-Forwarded-Server $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Url-Scheme $scheme;
   proxy_set_header X-Forwarded-Proto $scheme;
   proxy_set_header Host $http_host;
   proxy_redirect off;
   proxy_ssl_verify off;
   if ($request_method = POST ) {
   set $upstream '';
   access_by_lua '
   ngx.req.read_body()
   local  match0 = ngx.re.match(ngx.var.request_body, "aaa.*")
   local  match1 = ngx.re.match(ngx.var.request_body, "bbb.*")
   if match0 then
      ngx.var.upstream = "a"
   else
      ngx.var.upstream = "c"
   end
   ';
   proxy_pass http://$upstream;
   }
}
}

Objective: to distribute requests to upstreams depending on the passed request method. Requests are sent in json-rpc format. Problem: I have two upstreams. One upstream is available via the http protocol, the other via https and the problem that I am not experiencing is the following, at one time only 1 upstream works. Even if you create separate similar configs, only 1 is available and 1 will work. If anyone came across, help, please. How can you specify multiple upstreams that are available through different protocols? Or I did not register the config correctly? Even the specified directive "proxy_ssl_verify off" in the openresty settings does not help Thanks in advance.


Solution

  • Take a look at my answer here https://stackoverflow.com/a/64331014/2060502

    You may configure 2 internal locations with different upstreams and proxy_* derectives and do an internal redirect by ngx.exec() Lua API, preferably in context of rewrite_by_lua_block