Nginx Lua HTTP客户端

发布时间:January 12, 2014 // 分类:Nginx // No Comments

1
git clone https://github.com/liseen/lua-resty-http.git

Nginx配置:
1
2
3
4
5
6
7
8
http
{
    llua_package_path "/path/to/lua-resty-http/lib/?.lua;;";
    server {
        location /test {
            content_by_lua_file '/usr/local/nginx/conf/lua/proxy.lua';
        }
}

Proxy.lua内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
local http = require "resty.http"
local hc = http:new()
local url = "http://"
if ngx.var.http_host then
    url = url .. ngx.var.http_host 
end
url = url .. ngx.var.request_uri  -- 拼接完整的URL
if ngx.var.args then
    url = url .. "?" .. ngx.var.args
end
--[[
local ok, code, headers, status, body = hc:proxy_pass {
url = url,
proxy = "http://192.168.1.5:8118",
timeout = 3000,
headers = ngx.req.get_headers(), -- 传递客户端HEAD
method = ngx.var.request_method, -- 传递客户端method
method = "GET",
}
]]
local ok, code, headers, status, body = hc:request {
    url = url,
    proxy = "http://192.168.1.5:8118",
    timeout = 3000,
    headers = {UserAgent = "Mozilla/5.0"},
    headers = ngx.req.get_headers(), 
    method = ngx.var.request_method,
}
ngx.say(ok)
ngx.say(code)
ngx.say(body)
ngx.say(url)

更多:http://wendal.net/422.html
::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com


订阅 substack 体验古早写作:


点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:
zoomquiet


自怼圈/年度番新

DU22.4
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号: 44049002000656 ...::