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
Thanks mate! I need to configure nginx/lua to use a HTTP proxy when sending the requests. The LuaSocket library allowed me to do this. I was a bit unclear if this is possible using lua-resty-http or lua-resty-http-simple based on the docs. Can this be done? The lua-resty-upstream module looks like it can be used to proxy requests, but its marked as experimental. Any thoughts on how to implement this?
On Thursday, May 15, 2014 11:45:44 AM UTC+3, James Hurst wrote:Hi,Thanks mate! I need to configure nginx/lua to use a HTTP proxy when sending the requests. The LuaSocket library allowed me to do this. I was a bit unclear if this is possible using lua-resty-http or lua-resty-http-simple based on the docs. Can this be done? The lua-resty-upstream module looks like it can be used to proxy requests, but its marked as experimental. Any thoughts on how to implement this?You can use the "generic" interface in https://github.com/pintsized/lua-resty-http to connect to a proxy rather than the actual origin host, and then send a request with the appropriate Host header. The "proxy" field in the LuaSocket implementation is just a convenience allowing you to override the connection parameters with a proxy instead.
Hi James,
Is there a code example you could point me to?
Another (newbie) question, I downloaded the lua-resty-http source from github and tried recompiling openresty using:
./configure --add-module=/usr/local/src/lua-resty-http
But that results in the following error:
./configure: error: no /usr/local/src/lua-resty-http/config was found
I'm sure I'm missing a simple step, any hints for compiling lua-resty-http on ubuntu 14.01?
No need to compile anything! The Makefile is a bit of a red herring (I only use it for running tests), but Yichun's build system uses these to move modules into the correct location as part of the official releases. Since lua-resty-http is not part of the official release, you need to either:1) Install it alongside the other Lua modules, e.g. in /usr/local/openresty/lualibor probably more sensibly...2) Keep third party Lua modules wherever you want, and include those paths in your lua_package_path directive in your Nginx config file (https://github.com/openresty/lua-nginx-module#lua_package_path), such as:
Hi James,
I don't follow. Assuming I use the more sensible option 2, how do I compile nginx with lua-resty-http support? If I understood you correctly, once the module is compiled I could use it from the /usr/local/src/lua-resty-http directory via the lua_package_path directive. But how do I get past the initial compilation error about the missing config file?
So you can freely add / remove Lua modules to a compiled and installed OpenResty system without recompilation (Lua modules are not the same as Nginx modules written in C). You only need to ensure that they can be found in the lua_package_path directive (this can be any paths you like, just manually copy the module somewhere sensible and update that directive to match), and reload Nginx.Hope that helps,
FWIW, I also tried without /lib, yet still get this error:
2014/05/15 17:04:20 [error] 20110#0: *429 lua entry thread aborted: runtime error: /usr/local/openresty/nginx/conf/test.lua:69: attempt to index global 'http' (a nil value)
stack traceback:
coroutine 0:
Line 69 of test.lua is part of the HTTP function as shown below:
function HTTP()
local httpc = http.new()
local res, err = httpc:request_uri("http://checkip.amazonaws.com", {
method = "GET",
})
ngx.status = res.status
for k,v in pairs(res.headers) do
--
end
ngx.say(res.body)
end
Any hints as to what I'm doing wrong?