I have a request body that contains some numbers in JSON format. I need to forward that number to a specific host.

For example, I have 4 .csv files locally and I need just search if it's containing that number.

CSV files should compare the entries with an actual request body

file 1 - contains number 1 - if it's found in that file it should forward the request to host 1

file 2 - contains number 2 - if it's found in that file it should forward the request to host 2

file 3 - contains number 3 - if it's found in that file it should forward the request to host 3

file 4 - contains number 4 - if it's found in that file it should forward the request to host 4

Let me know if that is possible with lua nginx?

Update config 1

    location /pathcall {
       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 '';
       rewrite_by_lua '
       ngx.req.read_body()
       local data = ngx.req.get_body_data()
       1_file = "/opt/DATA/1.csv"
       2_file = "/opt/DATA/2.csv"
       file_1 = io.open(1_file)
       1_data = file_1:read()
       io.close(file_1)
       1_outputdata = ngx.say(1_data)
       file_2 = io.open(2_file)
       2_data = file_2:read()
       io.close(file_2)
       local 2_outputdata = ngx.say(2_data)
       local  match0 = ngx.re.match(ngx.var.request_body, ngx.var.1_data )
       local  match1 = ngx.re.match(ngx.var.request_body, ngx.var.2_data  )
    
            if match0 then
            ngx.var.upstream="host1"
            elseif match1 then
            ngx.var.upstream="host2"
            else
            ngx.var.upstream="host3"
       end
       ';
       proxy_pass https://$upstream ;
       }
    }
}

Seems it's not working still

    Write a Reply...