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