[root@nginx-cluster conf.d]# cat /usr/local/openresty/nginx/lua/ip_acl.lua local iputils = require("resty.iputils") iputils.enable_lrucache() local whitelist_ips = { "10.10.10.0/24", } whitelist = iputils.parse_cidrs(whitelist_ips)
access阶段进行控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
server { listen 8086; location /sayHi { access_by_lua_block { local iputils = require("resty.iputils") local res, err = iputils.ip_in_cidrs(ngx.var.remote_addr, whitelist) if not res then ngx.log(ngx.INFO, "ip acl err: ", err) return ngx.exit(ngx.HTTP_FORBIDDEN) end } content_by_lua_block { ngx.say("hello xadocker!") } } }
access_by_lua_block { local iputils = require("resty.iputils") local res, err = iputils.ip_in_cidrs(ngx.var.remote_addr, blacklist) if res then ngx.log(ngx.INFO, "ip acl err: ", err) return ngx.exit(ngx.HTTP_FORBIDDEN) end }
access_by_lua_block { local iputils = require("resty.iputils") local headers=ngx.req.get_headers() local ip = headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr local res, err = iputils.ip_in_cidrs(ip, whitelist) if not res then ngx.log(ngx.INFO, "ip acl err: ", err) return ngx.exit(ngx.HTTP_FORBIDDEN) end }