0

I've been implementing a kong plugin that needs to make HTTP requests to retrieve information to share it with the upstream services.

There is an excellent library called lua-resty-http that can be used to make HTTP requests.

The service that contains the information needed, it is configured behind the proxy, and it matches the path: /endpoint-providing-info.

The goal is to rely on the proxy capabilities to avoid having to parse the hostname which has some particular form that is not relevant to this question.

By playing around, I was able to achieve the desired behavior by doing the following:

local ok, err = http_client:connect("127.0.0.1", ngx.var.server_port) if not ok and err then return nil, 'there was a failure opening a connection: ' .. err local res, err = http_client:request({ method = 'GET', path = '/endpoint-providing-info' }) //parse the response, etc... 

The request gets routed to the upstream service and works as expected.

My primary concern is the following:

By connecting to localhost, I assumed that the current Nginx node is the one attending the request. Will this affect the performance? Is it better/possible to connect to the cluster directly?

1 Answer 1

1

I suppose that you configure for current nginx a location which match /endpoint-providing-info, use proxy module and configure an upstream for a cluster.

If you would use lua-resty-http:

Pros - you may use body_reader - an iterator function for reading the body in a streaming fashion.

Cons - your request will go thru kernel boundary (loopback interface).

Another possibility is to issue a subrequest using ngx.location.capture API

Pros - subrequests just mimic the HTTP interface but there is no extra HTTP/TCP traffic nor IPC involved. Everything works internally, efficiently, on the C level.

Cons - it is full buffered approach, will not work efficiently for big responses.

Update - IMO:

If you expect from upstream server big responses -lua-resty-http is your choice.

If you expect from upstream server a lot of small responses - ngx.location.capture should be used.

Sign up to request clarification or add additional context in comments.

2 Comments

lua-resty-http is way more convenient. But I'm still wondering if the cons you list there, is something that could cause some potential problems. I'll assume is not, since we are just adding an extra request to the same node attending it.
I have added my opinion into my answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.