@dooy
2016-11-25T07:40:53.000000Z
字数 3860
阅读 226
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 luajit
local bit = require("bit")
function byteToUint32(a,b,c,d)
local _int = 0
if a then
_int = _int + bit.lshift(a, 24)
end
_int = _int + bit.lshift(b, 16)
_int = _int + bit.lshift(c, 8)
_int = _int + d
if _int >= 0 then
return _int
else
return _int + math.pow(2, 32)
end
end
local 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 then
return nil
end
local 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 * 4
local 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 - 1028
start = start_len * 8 + 1024 + 1
local find_uint32 = 0
local index_offset = -1
local index_length = -1
local min= (start-1)/8
local 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 end
while 1 do
cnt=cnt+1
start= mid*8+1
find_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 then
min=mid
elseif ip_uint32 < find_uint32 then
max = mid
else
mark= mid
break;
end
local mid_new,yu = math.modf( (min+max)/2);
if mid == mid_new then
mark= max;
break;
end
mid = mid_new
end
start = 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 do
find_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 then
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)
break
end
start = start + 8
end
--]]
if index_offset == -1 or index_length == -1 then
return "hello"
end
local offset = offset_len + index_offset - 1024
file:seek("set", offset)
local restr= file:read(index_length);
return restr.."\tcnt="..cnt
end
--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 then
return
end
local args = ngx.req.get_uri_args()
local ip = ngx.var.remote_addr
if args["ip"] then
ip = args["ip"]
end
local ipstr= IpLocation(ip);
ngx.log(ngx.ERR,"IpLocation:",ipstr )
local pos,len= string.find(ipstr,"台湾")
if pos ~= nil then
ngx.log(ngx.ERR," to twpigai");
ngx.redirect("http://tw.pigai.org", 302)
--return ngx.exit(403)
end
local pos,len= string.find(ipstr,"香港")
if pos ~= nil then
ngx.log(ngx.ERR," to twpigai");
ngx.redirect("http://tw.pigai.org", 302)
--return ngx.exit(403)
end
local pos,len= string.find(ipstr,"澳门")
if pos ~= nil then
ngx.log(ngx.ERR," to twpigai");
ngx.redirect("http://tw.pigai.org", 302)
--return ngx.exit(403)
end
return