Hello!
On Wed, May 14, 2014 at 1:23 PM, Gordon Madarm wrote:
> I'm creating an API that should take a parameter and query several other
> external URIs using the submitted parameter value. For example, the api
> receives a request for /api?mode=123 then nginx should submit a request to
> http://ExternalServer.com/?status=123 and parse the response body (e.g.:
> <status>On</status>). Can this be done using subrequests or is it more
> efficient to use a lua script with socket.http to initiate the request (or
> is there a better way)?
>
Are you using the socket.http module from the LuaSocket library? If
yes, then it will horribly block your nginx event loop. And you should
avoid that.
Better use those 3rd-party lua-resty-http libraries based on ngx_lua's
nonblocking cosocket API out there, for instance,
https://github.com/pintsized/lua-resty-http
or
https://github.com/bakins/lua-resty-http-simple
These libraries are usually more efficient than subrequests, both
memory-wise and CPU-wise. But you should always do your own benchmark
for your own use case anyway :)
Regards,
-agentzh