Nginx使用Lua


文章來自
Nginx安裝lua支持
openresty
黑名單

前提條件
安裝LuaJIT,查看【工具/LuaJIT】筆記

Nginx-Lua支持

  • 下載解壓ngx_devel_kit
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -xzvf v0.3.0.tar.gz
  • 下載解壓lua-nginx-module
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz
tar -xzvf v0.10.8.tar.gz
  • 配置,編譯,安裝
# 前面的步驟查看【nginx】筆記

#注意ngx_devel_kit和lua-nginx-module以實際解壓路徑為准
./configure 
--add-module=/opt/soft/ngx_devel_kit-0.3.0 
--add-module=/opt/soft/lua-nginx-module-0.10.8
--prefix=/home/admin/server/nginx

make
make install

配置文件

  • 把命令寫在配置文件里
location /hello {
   default_type 'text/plain';
   content_by_lua 'ngx.say("hello, lua")';
   charset utf-8;
}
  • 把命令外部配置
#lua文件方式
#在server 中添加一個localtion
location /lua {
    default_type 'text/html';
    lua_code_cache off; # 把緩存關了,這樣修改lua文件就不用重啟nginx了
    content_by_lua_file conf/lua/test.lua; #相對於nginx安裝目錄
    charset utf-8;
}
# conf/lua/test.lua
ngx.say("hello world");
  • 服務啟動后訪問地址就行

大佬封裝好的工具包

功能插件使用,需要提前安裝在LuaJIT才行

  • 在server上添加配置
# 這個?是固定寫法
# 自己寫的或者大神寫的lua文件
lua_package_path "/usr/local/lualib/?.lua;;";
# 官方的插件
lua_package_cpath "/usr/local/lualib/?.so;;";

service {
   ...
}
  • 使用
# 引入cjson,這個不需要文件路徑,安裝cjson查看【LuaJIT】筆記
local cjson = require "cjson"

# 引入大佬的工具包
# 文件夾resty加里面的lua文件名
local redis = require "resty.redis"
local mysql = require "resty.mysql"
...

獲取訪問nginx的請求數據

  • Get
local method = ngx.var.request_method;
local headers = ngx.req.get_headers();
local uri_args = ngx.req.get_uri_args();
 
if method == "GET" then
    ngx.say("id:", uri_args["id"]);
    ngx.say("gender:", uri_args["gender"]);
    ngx.say("user-agent:", headers["user-agent"]);
end
  • Post
local cjson = require "cjson";
local method = ngx.var.request_method;
 
if method == "POST" then
    ngx.req.read_body();
    local data = ngx.req.get_body_data();
    local reqObj = cjson.decode(data);
    ngx.say("username:", reqObj["username"]);
    ngx.say("password:", reqObj["password"]);
end

黑名單,需要配合redis使用

  • 設置黑名單緩存,不用每個請求都去獲取一次
http {
    # 緩存大小5m
    lua_shared_dict forbidden_list 5m;
    
    location /lua {
        # lua_code_cache off;
        access_by_lua_file conf/lua/forbidden_list.lua;
        default_type 'text/html';
        content_by_lua 'ngx.say("hello world")';
    }
}
  • lua
local redis = require("resty.redis")
local ngx_log = ngx.log
local ngx_ERR = ngx.ERR
local ngx_INFO = ngx.INFO
local ngx_exit = ngx.exit
local ngx_var = ngx.var

-- 黑名單緩存60秒
local cache_idle = 60
local forbidden_list = ngx.shared.forbidden_list

local function close_redis(red)
	if not red then
		return
	end
	-- 釋放連接(連接池實現)
	local pool_max_idle_time = 10000 -- 毫秒
	local pool_size = 100  -- 連接池大小
	local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
	
	if not ok then
		ngx_log(ngx_ERR, "set redis keepalive error : ", err)
	end
end

-- 從redis獲取ip黑名單列表
local function get_forbidden_list()
	local red = redis:new()
	red:set_timeout(1000)
	local ip = "127.0.0.1"
	local port = 6379
	local password = "password"
	local ok, err = red:connect(ip, port)
	if not ok then
		ngx_log(ngx_ERR, "connect to redis error : ", err)
		close_redis(red)
		return
	end
	
	local res, err = red:auth(password)
    if not res then
        ngx_log(ngx_ERR, "failed to authenticate: ", err)
		close_redis(red)
        return
    end
	
	local resp, err = red:smembers("forbidden_list")
	if not resp then
		ngx_log(ngx_ERR, "get redis connect error : ", err)
		close_redis(red)
		return
	end
	-- 得到的數據為空處理
	if resp == ngx.null then
		resp = nil
	end
	close_redis(red)
	
	return resp
end

-- 刷新黑名單
local function reflush_forbidden_list()
	local current_time = ngx.now()
	local last_update_time = forbidden_list:get("last_update_time");
	
	if last_update_time == nil or last_update_time < (current_time - cache_idle) then
		local new_forbidden_list = get_forbidden_list();
		if not new_forbidden_list then
			return
		end
		
		forbidden_list:flush_all()
		for i, forbidden_ip in ipairs(new_forbidden_list) do
			forbidden_list:set(forbidden_ip, true);
		end
		forbidden_list:set("last_update_time", current_time);
	end
end


reflush_forbidden_list()
local ip = ngx_var.remote_addr
if forbidden_list:get(ip) then
	ngx_log(ngx_INFO, "forbidden ip refused access : ", ip)
	return ngx_exit(ngx.HTTP_FORBIDDEN)
end
  • 手動執行redis操作
# 添加黑名單,然后訪問
SADD forbidden_list "127.0.0.1"

# 移除黑名單,然后訪問
SREM forbidden_list "127.0.0.1"

動態黑名單思路
logstash+elasticsearch的組合,實現logstash實時讀取nginx的訪問日志access.log,elasticsearch儲存並聚合訪問日志中的訪問記錄,再由一個分析程序定時統計分析訪問記錄后判斷出要加入黑名單的ip,然后將ip儲存到redis中的ip黑名單列表


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM