@dooy
2016-11-25T07:40:53.000000Z
字数 3860
阅读 311
Nginx 批改网
参考 https://www.zybuluo.com/dooy/note/318900
从 http://www.ipip.net/download.html 下载IP库
将包中的 17monipdb.bat 发到/usr/local/nginx/lua/17monipdb.dat
将2个代码文件都放到 /usr/local/nginx/lua/下 在配置nginx
首先在nginx.conf http 中加入
lua_shared_dict config 1m;init_by_lua_file /usr/local/nginx/lua/ipmid.lua;
然后找到 location ~ .*\.php?$
在 他的区域里面加入
access_by_lua_file /usr/local/nginx/lua/istw.lua;
表示只有匹配php的时候才去查下IP;css 图片 html 这些就不去查下ip了
有2个比较重要的文件ipmid.lua 和 istw.lua
#!/usr/bin/env luajitlocal bit = require("bit")function byteToUint32(a,b,c,d)local _int = 0if a then_int = _int + bit.lshift(a, 24)end_int = _int + bit.lshift(b, 16)_int = _int + bit.lshift(c, 8)_int = _int + dif _int >= 0 thenreturn _intelsereturn _int + math.pow(2, 32)endendlocal ipBinaryFilePath = "/usr/local/nginx/lua/17monipdb.dat"function IpLocation(ipstr)local ip1,ip2,ip3,ip4 = string.match(ipstr, "(%d+).(%d+).(%d+).(%d+)")local ip_uint32 = byteToUint32(ip1, ip2, ip3, ip4)local file = io.open(ipBinaryFilePath)if file == nil thenreturn nilendlocal str = file:read(4)local offset_len = byteToUint32(string.byte(str, 1), string.byte(str, 2),string.byte(str, 3),string.byte(str, 4))local indexBuffer = file:read(offset_len - 4)local tmp_offset = ip1 * 4local start_len = byteToUint32(string.byte(indexBuffer, tmp_offset + 4), string.byte(indexBuffer, tmp_offset + 3), string.byte(indexBuffer, tmp_offset + 2), string.byte(indexBuffer, tmp_offset + 1))local max_comp_len = offset_len - 1028start = start_len * 8 + 1024 + 1local find_uint32 = 0local index_offset = -1local index_length = -1local min= (start-1)/8local max= max_comp_len/8;local mid,yu= math.modf( (min+max)/2);local mark = -1;local cnt=0--if start >0 then return cnt.." mark="..mark.."\tmin="..min.."\tstart="..start.."\tend="..max_comp_len endwhile 1 docnt=cnt+1start= mid*8+1find_uint32 = byteToUint32(string.byte(indexBuffer, start), string.byte(indexBuffer, start+1),string.byte(indexBuffer, start+2),string.byte(indexBuffer, start+3))if ip_uint32 > find_uint32 thenmin=midelseif ip_uint32 < find_uint32 thenmax = midelsemark= midbreak;endlocal mid_new,yu = math.modf( (min+max)/2);if mid == mid_new thenmark= max;break;endmid = mid_newendstart = mark*8+1;index_offset = byteToUint32(0, string.byte(indexBuffer, start+6),string.byte(indexBuffer, start+5),string.byte(indexBuffer, start+4))index_length = string.byte(indexBuffer, start+7)--[[while start < max_comp_len dofind_uint32 = byteToUint32(string.byte(indexBuffer, start), string.byte(indexBuffer, start+1),string.byte(indexBuffer, start+2),string.byte(indexBuffer, start+3))if ip_uint32 <= find_uint32 thenindex_offset = byteToUint32(0, string.byte(indexBuffer, start+6),string.byte(indexBuffer, start+5),string.byte(indexBuffer, start+4))index_length = string.byte(indexBuffer, start+7)breakendstart = start + 8end--]]if index_offset == -1 or index_length == -1 thenreturn "hello"endlocal offset = offset_len + index_offset - 1024file:seek("set", offset)local restr= file:read(index_length);return restr.."\tcnt="..cntend--print(IpLocation("210.34.3.4"))
--ngx.log(ngx.ERR,"LOOKIP:", ngx.req.get_headers()["Host"], "\thost:",ngx.var.host,"\tmethod=", ngx.req.get_method() ,"\trq_uri=", ngx.var.request_uri );local _isgo= ngx.req.get_method()=="GET" and (ngx.var.request_uri=="/" or ngx.var.request_uri=="/index.php" ) and (ngx.var.host=="pigai.org" or ngx.var.host=="117.79.131.103" )if true ~= _isgo thenreturnendlocal args = ngx.req.get_uri_args()local ip = ngx.var.remote_addrif args["ip"] thenip = args["ip"]endlocal ipstr= IpLocation(ip);ngx.log(ngx.ERR,"IpLocation:",ipstr )local pos,len= string.find(ipstr,"台湾")if pos ~= nil thenngx.log(ngx.ERR," to twpigai");ngx.redirect("http://tw.pigai.org", 302)--return ngx.exit(403)endlocal pos,len= string.find(ipstr,"香港")if pos ~= nil thenngx.log(ngx.ERR," to twpigai");ngx.redirect("http://tw.pigai.org", 302)--return ngx.exit(403)endlocal pos,len= string.find(ipstr,"澳门")if pos ~= nil thenngx.log(ngx.ERR," to twpigai");ngx.redirect("http://tw.pigai.org", 302)--return ngx.exit(403)endreturn