proxy_pass in Openresty

124 views
Skip to first unread message

Amir Salar Pourhasan

unread,
Jul 16, 2019, 6:03:32 AM7/16/19
to openresty-en
I want to check something in Lua and then proxy_pass in my target using Nginx code but does not work!

location /nt {
    default_type
'text/plain';
    content_by_lua_file check_parameters
.lua;
}

In check_parameters.lua:

-- Client IP
local client_ip = ngx.var.remote_addr or ''


-- check IP blockage
local is_ip_allowed = check_ip( client_ip )
if is_ip_allowed == true then
   
local url_params = extract_info( ngx.var.request_uri )

   
-- check has parameters
   
if (url_params['username'] == nil) or (url_params['password'] == nil) or (url_params['username'] == '') or (url_params['password'] == '') then
        ngx
.say('Invalid request!')
   
else
       
-- Redis query key
       
local user_redis_key = "line_" .. url_params['username'] .. '_' .. url_params['password']

       
local searched_user = redis_find( user_redis_key ) -- by function in another file

       
if searched_user['status'] == false then
            ngx
.say( searched_user['msg'] )
       
else
           
           
-- user found in redis
            ngx
.say('Request is fine! lets proxy_pass it to main server')


           
-- ************** HERE I NEED TO PASS REQUEST TO THE MAIN SERVER BECAUSE THE REQUEST IS VALID. BUT HOW?????????????

       
end
   
end
else
   
--  IP blocked
    ngx
.say('Access forbidden')
end


How can I proxy_pass in Lua file?

Igor Clark

unread,
Jul 16, 2019, 7:03:33 AM7/16/19
to openre...@googlegroups.com
Hi Amir,

If I understand correctly you want to restrict access to the proxies resource using your Lua code below. Check out access_by_lua_file https://github.com/openresty/lua-nginx-module/blob/master/README.markdown#access_by_lua_file - you can run your auth checks in the access_by_lua_file block. Exit with 401/403 (as appropriate) if the checks fail, and return normally if not, then in the nginx config put the proxy_pass underneath the access_by_lua_file block. If all the checks pass, the execution should fall through to the proxy_pass block.

Cheers
Igor
--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openresty-en/b12a5929-cc44-4540-beec-43150cb9ea35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Amir Salar Pourhasan

unread,
Jul 16, 2019, 8:27:26 AM7/16/19
to openresty-en
Yeah it fixed. thanx
Reply all
Reply to author
Forward
0 new messages