On Mon, Aug 11, 2014 at 2:50 AM, Yichun Zhang (agentzh)
<age...@gmail.com> wrote:
> Hello!
>
> 2014-08-10 7:55 GMT-07:00 Zoom.Quiet:
>> 目测 当前 lua-resty-http 不支持 https
>> 那么可行的方案就是先手工用 cosocket 接口,构造一个 https 的客户端,
>> 这事儿,暂时超过俺的能力范畴,
>>
>
> ngx_lua 模块刚刚实现了 SSL cosocket API,尚未包含在现有的任何发布中,所以 lua-resty-http 也没有对
> https 的现成的支持。不过 lua-resty-http 有一个分支已经使用新的 SSL cosocket 支持实现了 https:
>
> https://github.com/hamishforbes/lua-resty-http/tree/ssl-cosocket
>
> 更多细节可以参见下面这个 ticket 里的讨论:
>
> https://github.com/pintsized/lua-resty-http/issues/11
>
soo happy 的节奏,不过,现在用不了,只能口水ing...
> 对于现有的 OpenResty 和 ngx_lua 发布,可以使用 ngx.location.capture + ngx_proxy 进行
> https 后端通信,见这里给出的一个通过 https 访问 Google 搜索的代码示例:
>
> https://github.com/openresty/lua-nginx-module/issues/178#issuecomment-48390360
>
嗯嗯嗯,这就是两年前,讨论线索最后推荐的 cosocket 数据流思路哪!
又参考了以下代码:
HttpLuaModule - Nginx Community
http://wiki.nginx.org/HttpLuaModule#ngx.location.capture
...
res = ngx.location.capture(
'/foo/bar',
{ method = ngx.HTTP_POST, body = 'hello, world' }
)
Posting to goo.gl from Android - Stack Overflow
http://stackoverflow.com/questions/6052156/posting-to-goo-gl-from-android
...
post.setEntity(new StringEntity("{\"longUrl\": \"http://www.google.com/\"}"));
...
post.setHeader("Content-Type", "application/json");
解决了问题 ;-)
配置中追加:
location /googleapis {
internal;
rewrite ^/googleapis(.*) $1 break;
# to disable gzip compression on the backend
proxy_set_header Accept-Encoding '';
proxy_set_header Content-Type 'application/json';
#proxy_set_header contentType 'application/x-www-form-urlencoded';
proxy_pass https://www.googleapis.com;
}
lua 中变成:
SURI = "https://www.googleapis.com/urlshortener/v1/url"
ngx.say("uri\t", URI)
--DATA = "longUrl=" .. URI
--JSON = '{"longUrl"="' .. URI .. '"}'
local cjson = require "cjson"
JSON = cjson.encode({longUrl=URI})
ngx.say("JSON\t", JSON)
-- search for "openresty" in google over https:
local res = ngx.location.capture(
"/googleapis/urlshortener/v1/url",
{ method = ngx.HTTP_POST,
body = JSON }
)
if res.status ~= 200 then
ngx.say("failed to query google: ", res.status, ": ", res.body)
return
end
-- here we just forward the Google search result page intact:
ngx.header["Content-Type"] = "application/json; charset=UTF-8"
ngx.say(res.body)
测试成功:
$ curl -d "uri=https://docs.google.com/forms/d/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU/viewform"
23.239.0.15:10080/=/goo
request_method: POST
args.uri: https://docs.google.com/forms/d/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU/viewform
uri https://docs.google.com/forms/d/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU/viewform
JSON {"longUrl":"https:\/\/docs.google.com\/forms\/d\/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU\/viewform"}
{
"kind": "urlshortener#url",
"id": "http://goo.gl/fiAfLo",
"longUrl": "https://docs.google.com/forms/d/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU/viewform"
}
so happy now,
俺将合并这个小故事到俺的乱入手册中,
彻底放弃 luacurl 的方案.
thanx for all.
> Regards,
> -agentzh
>
--
人生苦短, Pythonic! 冗余不做,日子甭过!备份不做,十恶不赦!
KM keep growing environment culture which promoting organization be learnning!
俺: http://zoomquiet.io
许: http://creativecommons.org/licenses/by-sa/2.5/cn/