1

Problem Statement

I would like to proxy_pass to another url based on the some value present in the request header. All the request details including query parameter(if any), header and everything should be passed to the proxy address.

What i have tried

I have followed SO post and based on the requirement mentioned i have tried the following.

Test.lua file

local token = ngx.var.http_Authorization
if token == "hello"
then 
-- ngx.print ("hello")
local res = ngx.location.capture("/someURL") 
if res.status == 200
then ngx.print(res.body)
end
end

nginx.conf

location /api/employees {
        content_by_lua_file test.lua;
}
location /someURL {
    internal;
    proxy_pass http://productionURL?q1=123&name=xyz;
    #proxy_redirect default;
}

As you can see i am passing the query parameter manually in the proxy_pass statement.

How do i resolve this by not passing actual request during proxy_pass ?

1 Answer 1

2

Your problem is easily resolved by rewriting the original request depending of the header. Here's an example:

#sample backend
set $backend_host "http://httpbin.org";

location ~*/api/employees {
        rewrite_by_lua '
             --reading request headers
             local req_headers = ngx.req.get_headers()
             local target_uri  = ""
             -- checking an a header value to determine target uri
             if req_headers["x-special-header"] then
                target_uri = req_headers["x-special-header"]
             else 
                -- default path if not header found
                target_uri = "/get"
             end 
            ngx.log(ngx.NOTICE, string.format("resolved target_uri: %s", target_uri))
            --rewriting uri according to header (all original req args and headers are preserved)
            ngx.req.set_uri(target_uri)
        ';
        proxy_pass $backend_host;
}

Sample request sending 'special' header as the path for the target backend:

curl 'http://localhost/api/employees?arg1=val1&arg2=val2' -H 'x-special-header: /headers'

Response:

    {
  "headers": {
    "Accept": "*/*",
    "Connect-Time": "0",
    "Connection": "close",
    "Host": "httpbin.org",
    "Total-Route-Time": "0",
    "User-Agent": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2",
    "Via": "1.1 vegur",
    "X-Request-Id": "6e515e0a-0061-4576-b1aa-5da3e3308c81",
    "X-Special-Header": "/headers"
  }

Sample request without 'special' header:

curl 'http://localhost/api/employees?arg1=val1&arg2=val2'

Response:

{
  "args": {
    "arg1": "val1",
    "arg2": "val2"
  },
  "headers": {
    "Accept": "*/*",
    "Connect-Time": "4",
    "Connection": "close",
    "Host": "httpbin.org",
    "Total-Route-Time": "0",
    "User-Agent": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2",
    "Via": "1.1 vegur",
    "X-Request-Id": "520c0e12-1361-4c78-8bdf-1bff3f9d924c"
  },
  "url": "http://httpbin.org/get?arg1=val1&arg2=val2"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.