location /lua { set $allowed_ips ' 58.0.0.0 59.0.0.0 '; access_by_lua_file /etc/nginx/lua-scripts/access_limit.lua; echo "Hello, Lua"; }
local client_ip = ngx.req.get_headers()["X-Forwarded-For"] client_ip = client_ip or ngx.var.remote_addr local passed = false for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do if client_ip == allowed_ip then passed = true end end if not passed then ngx.exit(ngx.HTTP_FORBIDDEN) end
刚开始用nginx+lua干活儿, 分享一个样例, 欢迎指正.nginx: location /lua { set $allowed_ips ' 58.0.0.0 59.0.0.0 '; access_by_lua_file /etc/nginx/lua-scripts/access_limit.lua; echo "Hello, Lua"; } lua script:local client_ip = ngx.req.get_headers()["X-Forwarded-For"] client_ip = client_ip or ngx.var.remote_addr local passed = false for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do if client_ip == allowed_ip then passed = true end end if not passed then ngx.exit(ngx.HTTP_FORBIDDEN) end --
> for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do这里的string.gmatch修改成ngx.re.gmatch方式,性能会更好。原型:syntax: iterator, err = ngx.re.gmatch(subject, regex, options?)地址:http://wiki.nginx.org/HttpLuaModule#ngx.re.gmatch如果这个请求量级非常大,在option部分使用'jo'可以进一步提高速度。感谢分享。On Sat, Jul 11, 2015 at 2:54 PM, wonderbeyond Zhang <wonde...@gmail.com> wrote:刚开始用nginx+lua干活儿, 分享一个样例, 欢迎指正.nginx: location /lua { set $allowed_ips ' 58.0.0.0 59.0.0.0 '; access_by_lua_file /etc/nginx/lua-scripts/access_limit.lua; echo "Hello, Lua"; } lua script:local client_ip = ngx.req.get_headers()["X-Forwarded-For"] client_ip = client_ip or ngx.var.remote_addr local passed = false for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do if client_ip == allowed_ip then passed = true end end if not passed then ngx.exit(ngx.HTTP_FORBIDDEN) end -- -- MembhisMy github: https://github.com/membphis --
多谢指点!我最近才开始尝试写Lua, 都是凭在其它语言建立的感觉, 然后搜索一下lua里面对应的方法, 还没建立足够的自信.以后有啥样例可以继续分享交流. ☺在 2015年7月11日 下午3:03,YuanSheng Wang <mem...@gmail.com>写道:> for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do这里的string.gmatch修改成ngx.re.gmatch方式,性能会更好。原型:syntax: iterator, err = ngx.re.gmatch(subject, regex, options?)地址:http://wiki.nginx.org/HttpLuaModule#ngx.re.gmatch如果这个请求量级非常大,在option部分使用'jo'可以进一步提高速度。感谢分享。On Sat, Jul 11, 2015 at 2:54 PM, wonderbeyond Zhang <wonde...@gmail.com> wrote:刚开始用nginx+lua干活儿, 分享一个样例, 欢迎指正.nginx: location /lua { set $allowed_ips ' 58.0.0.0 59.0.0.0 '; access_by_lua_file /etc/nginx/lua-scripts/access_limit.lua; echo "Hello, Lua"; } lua script:local client_ip = ngx.req.get_headers()["X-Forwarded-For"] client_ip = client_ip or ngx.var.remote_addr local passed = false for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do if client_ip == allowed_ip then passed = true end end if not passed then ngx.exit(ngx.HTTP_FORBIDDEN) end -- -- MembhisMy github: https://github.com/membphis -- -- work wonders together!
如果经过多层反向代理, X-Forwarded-For会是一个逗号列表, 之前的样例没有考虑, 完整版在这里: https://gist.github.com/wonderbeyond/d650c0dd9c74279c4128在 2015年7月11日 下午3:07,wonderbeyond <wonde...@gmail.com>写道:多谢指点!我最近才开始尝试写Lua, 都是凭在其它语言建立的感觉, 然后搜索一下lua里面对应的方法, 还没建立足够的自信.以后有啥样例可以继续分享交流. ☺在 2015年7月11日 下午3:03,YuanSheng Wang <mem...@gmail.com>写道:> for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do这里的string.gmatch修改成ngx.re.gmatch方式,性能会更好。原型:syntax: iterator, err = ngx.re.gmatch(subject, regex, options?)地址:http://wiki.nginx.org/HttpLuaModule#ngx.re.gmatch如果这个请求量级非常大,在option部分使用'jo'可以进一步提高速度。感谢分享。On Sat, Jul 11, 2015 at 2:54 PM, wonderbeyond Zhang <wonde...@gmail.com> wrote:刚开始用nginx+lua干活儿, 分享一个样例, 欢迎指正.nginx: location /lua { set $allowed_ips ' 58.0.0.0 59.0.0.0 '; access_by_lua_file /etc/nginx/lua-scripts/access_limit.lua; echo "Hello, Lua"; } lua script:local client_ip = ngx.req.get_headers()["X-Forwarded-For"] client_ip = client_ip or ngx.var.remote_addr local passed = false for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do if client_ip == allowed_ip then passed = true end end if not passed then ngx.exit(ngx.HTTP_FORBIDDEN) end -- -- MembhisMy github: https://github.com/membphis -- -- work wonders together! -- work wonders together! --
如果连 X-Forwarded-For 都认的话,那白名单也没什么意义了吧 :)Lance 2015-07-11 18:10 GMT+08:00 wonderbeyond <wonde...@gmail.com>:如果经过多层反向代理, X-Forwarded-For会是一个逗号列表, 之前的样例没有考虑, 完整版在这里: https://gist.github.com/wonderbeyond/d650c0dd9c74279c4128在 2015年7月11日 下午3:07,wonderbeyond <wonde...@gmail.com>写道:多谢指点!我最近才开始尝试写Lua, 都是凭在其它语言建立的感觉, 然后搜索一下lua里面对应的方法, 还没建立足够的自信.以后有啥样例可以继续分享交流. ☺在 2015年7月11日 下午3:03,YuanSheng Wang <mem...@gmail.com>写道:> for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do这里的string.gmatch修改成ngx.re.gmatch方式,性能会更好。原型:syntax: iterator, err = ngx.re.gmatch(subject, regex, options?)地址:http://wiki.nginx.org/HttpLuaModule#ngx.re.gmatch如果这个请求量级非常大,在option部分使用'jo'可以进一步提高速度。感谢分享。On Sat, Jul 11, 2015 at 2:54 PM, wonderbeyond Zhang <wonde...@gmail.com> wrote:刚开始用nginx+lua干活儿, 分享一个样例, 欢迎指正.nginx: location /lua { set $allowed_ips ' 58.0.0.0 59.0.0.0 '; access_by_lua_file /etc/nginx/lua-scripts/access_limit.lua; echo "Hello, Lua"; } lua script:local client_ip = ngx.req.get_headers()["X-Forwarded-For"] client_ip = client_ip or ngx.var.remote_addr local passed = false for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do if client_ip == allowed_ip then passed = true end end if not passed then ngx.exit(ngx.HTTP_FORBIDDEN) end -- -- MembhisMy github: https://github.com/membphis -- -- work wonders together! -- work wonders together! -- --
X-Forwarded-For 里面可以看到最终端的用户IP。公司内网的出口网关是几个固定的IP,可以做白名单的。在 2015年7月11日 下午8:04,Lance <lance...@gmail.com>写道:如果连 X-Forwarded-For 都认的话,那白名单也没什么意义了吧 :)Lance 2015-07-11 18:10 GMT+08:00 wonderbeyond <wonde...@gmail.com>:如果经过多层反向代理, X-Forwarded-For会是一个逗号列表, 之前的样例没有考虑, 完整版在这里: https://gist.github.com/wonderbeyond/d650c0dd9c74279c4128在 2015年7月11日 下午3:07,wonderbeyond <wonde...@gmail.com>写道:多谢指点!我最近才开始尝试写Lua, 都是凭在其它语言建立的感觉, 然后搜索一下lua里面对应的方法, 还没建立足够的自信.以后有啥样例可以继续分享交流. ☺在 2015年7月11日 下午3:03,YuanSheng Wang <mem...@gmail.com>写道:> for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do这里的string.gmatch修改成ngx.re.gmatch方式,性能会更好。原型:syntax: iterator, err = ngx.re.gmatch(subject, regex, options?)地址:http://wiki.nginx.org/HttpLuaModule#ngx.re.gmatch如果这个请求量级非常大,在option部分使用'jo'可以进一步提高速度。感谢分享。On Sat, Jul 11, 2015 at 2:54 PM, wonderbeyond Zhang <wonde...@gmail.com> wrote:刚开始用nginx+lua干活儿, 分享一个样例, 欢迎指正.nginx: location /lua { set $allowed_ips ' 58.0.0.0 59.0.0.0 '; access_by_lua_file /etc/nginx/lua-scripts/access_limit.lua; echo "Hello, Lua"; } lua script:local client_ip = ngx.req.get_headers()["X-Forwarded-For"] client_ip = client_ip or ngx.var.remote_addr local passed = false for allowed_ip in string.gmatch(ngx.var.allowed_ips, '([%d%.]+)') do if client_ip == allowed_ip then passed = true end end if not passed then ngx.exit(ngx.HTTP_FORBIDDEN) end -- -- MembhisMy github: https://github.com/membphis -- -- work wonders together! -- work wonders together! -- -- -- work wonders together! --
如果代理服务器都在公司内网,安全程度还可以接受。7层HTTP代理权,不能信任外部调用。 --