掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
由于公司機(jī)房和辦公環(huán)境是在一起的,默認(rèn)情況下公司出口IP是禁止80/443訪問(wèn)【運(yùn)營(yíng)商側(cè)有限制】。目前采用的是阿里云進(jìn)行中轉(zhuǎn),即將開發(fā)環(huán)境的域名解析到阿里云,然后通過(guò)Nginx反向代理到公司出口非80端口。開發(fā)環(huán)境部分接口涉及到第三方回調(diào)和校驗(yàn),所以完全禁止開發(fā)環(huán)境對(duì)外網(wǎng)訪問(wèn)不現(xiàn)實(shí)。

目前合理的需求如下:
面對(duì)上述簡(jiǎn)單的需求場(chǎng)景,我們?nèi)绾螌?shí)現(xiàn)呢?
方案一:采用防火墻白名單策略進(jìn)行實(shí)現(xiàn),目前看只能實(shí)現(xiàn) 1 和 2 的條件
方案二:采用Nginx的allow、deny等策略,目前看也只能實(shí)現(xiàn) 1 和 2 的條件
方案三:采用Nginx+Lua 通過(guò)access_by_lua_file策略,目前看能實(shí)現(xiàn)上述所有條件而且實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單,改造成本較小。
- root@develop:/usr/local/nginx/scripts# cat filter_white.lua
- -- 默認(rèn)配置
- local redis = require 'resty.redis'
- local allow = false
- -- 連接Redis
- local red = redis:new()
- local ok, err = red:connect('172.17.173.183', 26379)
- if not ok then
- ngx.log(ngx.ERR, 'connect to redis failed: ' .. err)
- end
- local res, err = red:auth('Huajianghu@123')
- if not res then
- ngx.log(ngx.ERR, 'failed to authenticate: ' .. err)
- end
- -- 過(guò)濾精確IP
- --if red:sismember('white:dev:ip', ngx.var.remote_addr) == 1 then
- -- allow = true
- --end
- -- 過(guò)濾IP地址段
- local iputils = require("resty.iputils")
- iputils.enable_lrucache()
- local white_ips =red:smembers('white:dev:ip')
- local whitelist = iputils.parse_cidrs(white_ips)
- if iputils.ip_in_cidrs(ngx.var.remote_addr, whitelist) then
- allow = true
- end
- -- 過(guò)濾URL
- if not allow then
- local url = ngx.var.http_host .. ngx.var.uri
- local white_urls = red:smembers('white:dev:url')
- for index, white_url in ipairs(white_urls) do
- if url:match(white_url) then
- allow = true
- break
- end
- end
- end
- -- 默認(rèn)策略
- if not allow then
- ngx.log(ngx.ERR, "not allow: " .. ngx.var.http_host .. ngx.var.uri)
- ngx.status = ngx.HTTP_FORBIDDEN
- ngx.say('請(qǐng)申請(qǐng)白名單')
- ngx.exit(200)
- end
3.此腳本僅供參考使用,特殊場(chǎng)景需要進(jìn)行修改lua腳本。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流