openresty / openresty/lua-nginx-module

NGINX Lua search in file and forward the request

Open
#2,161 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported rewrite_by_lua block, especially ngx.req.read_body, ngx.re.match, and proxy_pass, then inspect how it reads /opt/DATA/1.csv and /opt/DATA/2.csv. Done means determining why the posted configuration does not match the request body or select the intended host, and documenting a reproducible, supported path forward.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.