Hello!
On Sat, Oct 27, 2012 at 10:52 AM, Zhigang Chen wrote:
> In the past few days, I was researching how to use subrequest in nginx and come across a few very cool nginx modules you have contributed to. Thanks for sharing your work and expertise.
>
Thanks :)
> I find that you also work and live in SF. I do too. Maybe we can grab a lunch or sth together in the near future.
>
Yes, sure :)
> A question about the lua module: can ngx.location.capture make an request to an external url? Like, ngx.location.capture("http://www.google.com").
>
> I tried, but got the error:
>
> open() "/usr/local/nginx/htmhttp://www.google.com" failed.
>
> The subrequest was correct, though: subrequest: "http://www.google.com".
>
Nginx subrequests can only target Nginx locations. The error message
above indicates that Nginx is treating your "http://www.googleww.com"
as a location name.
To do external requests, you need to use the Nginx upstream module
within the subrequest. In this case, you need the standard ngx_proxy
module. Here's a complete example (tested on my side):
location /t {
content_by_lua '
local res = ngx.location.capture("/proxy", {
args = {
host = "www.google.com",
uri = "/search?q=Nginx%20Lua",
}
})
ngx.status = res.status
ngx.print(res.body)
';
}
location = /proxy {
internal;
# set_unescape_uri is provided by the ngx_set_misc module
set_unescape_uri $my_host $arg_host;
set_unescape_uri $my_uri $arg_uri;
resolver 8.8.8.8; # here we use google's public DNS server
proxy_set_header User-Agent 'Mozilla/5.0 (X11; Linux x86_64; rv:16.0)';
proxy_pass http://$my_host$my_uri;
}
Accessing location /t above will give you the Google Search result
page for the query "Nginx Lua" :)
BTW, I'm cc'ing the openresty-en mailing list:
https://groups.google.com/group/openresty-en So that other people may
also find the discussion here useful :) You're highly recommended to
join the list and post such questions there :)
Thanks!
-agentzh