lua示例代码

function get_client_ip()
    local headers=ngx.req.get_headers()
    local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"
    return ip
end

if "GET" == request_method then args = ngx.req.get_uri_args() elseif "POST" == request_method then ngx.req.read_body() args = ngx.req.get_post_args() end

local http = require "resty.http"
 local httpc = http.new()
 httpc:set_timeout(5000)
url = "http://127.0.0.1:8089/?ip="..get_client_ip();
local res, err = httpc:request_uri(url, {
 method = "GET",
 keepalive = false,
 headers = {
 ["Content-Type"] = "application/x-www-form-urlencoded",
 }
})
if not res then
        ngx.say("{\"status\":false,\"msg\":\"timeout\"}")
        return
end
ngx.say(res.body)

location = /
        {
                default_type 'text/plain';
                content_by_lua_file /app/nginx/conf/vhost/ip.lua;

        }

 

你可能感兴趣的:(lua)