当前位置: > Linux服务器 > nginx >

Nginx Lua HTTP客户端

时间:2016-12-18 10:07来源:linux.it.net.cn 作者:IT
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内容:

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)



(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容