由于公司机房和办公环境是在一起的,所以默认禁止公司出口IP访问80/443【运营商侧限制】。目前使用阿里云中转,即将开发环境的域名解析到阿里云,然后通过Nginx反向代理到公司的非80端口导出。开发环境部分接口涉及到第三方回调和验证,完全禁止开发环境访问外网是不现实的。目前合理要求如下:公司网络地址段可以不受限制的访问开发环境。允许部分第三方IP地址段加入白名单。如果第三方IP不固定,需要支持第三方回调URL。以上条件不禁止添加白名单。互联网。面对以上简单的需求场景,我们该如何实现呢?方案一:使用防火墙白名单策略来实现。目前只能实现条件1和2。方案二:使用Nginx的allow、deny等策略。目前只能实现条件1和条件2。方案三:通过access_by_lua_file策略使用Nginx+Lua。目前,以上条件都可以实现,而且实现起来比较简单,改造成本也比较小。Nginxserver层配置:access_by_lua_file'scripts/filter_white.lua'filter_white.lua脚本配置信息:root@develop:/usr/local/nginx/scripts#catfilter_white.lua--默认配置localredis=require'resty.redis'localallow=false--connectRedislocalred=redis:new()localok,err=red:connect('172.17.173.183',26379)ifnotokthenngx.log(ngx.ERR,'connecttoredisfailed:'..err)endlocalres,err=red:auth('Huajianghu@123')ifnotresthenngx.log(ngx.ERR,'failedtoauthenticate:'..err)end--过滤精确IP--ifred:sismember('white:dev:ip',ngx.var.remote_addr)==1then--allow=true--end--过滤IP地址段localiputils=require("resty.iputils")iputils.enable_lrucache()localwhite_ips=red:smembers('white:dev:ip')localwhitelist=iputils.parse_cidrs(white_ips)ifuputils.ip_in_cidrs(ngx.var.remote_addr,whitelist)thenallow=trueend--过滤URLifnotallowthenlocalurl=ngx.var.http_host..ngx.var.urilocalwhite_urls=red:smembers('white:dev:url')forindex,white_urlinipairs(white_urls)doifurl:match(white_url)thenallow=truebreakendendend--默认Policyifnotallowthenngx.log(ngx.ERR,"notallow:"..ngx.var.http_host..ngx.var.uri)ngx.status=ngx.HTTP_FORBIDDENngx.say('请申请白名单')ngx.exit(200)结束3。本脚本仅供参考,特殊场景需要修改lua脚本
