Skip to content

Commit 537089b

Browse files
authored
Create lua-ngx-log.md
添加Lua处理Nginx日志脚本
1 parent 847c149 commit 537089b

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

ops-scripts/lua-ngx-log.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#### 1. install softs
2+
3+
```
4+
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz
5+
wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gz
6+
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1.tar.gz
7+
wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
8+
```
9+
#### 2. install openrestry luajit2 lib
10+
```
11+
./configure --prefix=/user/local/openresty-1.15.8.1
12+
make
13+
make install
14+
ln -s /usr/local/openresty-1.15.8.1/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
15+
export LUAJIT_LIB=/usr/local/openresty-1.15.8.1/luajit/lib/
16+
export LUAJIT_INC=/usr/local/openresty-1.15.8.1/luajit/include/luajit-2.1/
17+
18+
```
19+
#### 3. install tengine
20+
21+
```
22+
./configure --prefix=/usr/local/tengine/ --with-http_gzip_static_module --add-module=/data/data/softs/lua-nginx-module-0.10.15/ --add-module=/data/data/softs/ngx_devel_kit-0.3.1/
23+
make
24+
make install
25+
```
26+
27+
#### 4. Nginx Configure: log_by_lua_file
28+
```
29+
log_by_lua_file /usr/local/tengine/conf/lua/ngx_log.lua;
30+
```
31+
32+
### 5. ngx_log.lua
33+
```
34+
local file_name = '/usr/local/tengine/logs/mapi.ipaylinks.com.audit.log'
35+
36+
function write_content(fileName, content)
37+
local f = assert(io.open(fileName,'a'))
38+
f:write(content)
39+
f:close()
40+
end
41+
42+
function urlDecode(s)
43+
if(nil ~= s)
44+
then
45+
s = string.gsub(s, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
46+
return s
47+
end
48+
end
49+
50+
function handle_body(s)
51+
s = urlDecode(s)
52+
if(nil ~= s)
53+
then
54+
return string.gsub(s,'payMethodInfo=(.-)&','payMethodInfo=***&')
55+
end
56+
end
57+
58+
59+
local extend = string.format('srcip=%s&x_srcip=%s&time="%s"&server=%s&server_ip=%s&method=%s&link="%s://%s%s&status=%s&referer="%s"&post="%s"&user_agent="%s"',ngx.var.remote_addr,ngx.var.http_x_forwarded_for,ngx.var.time_local,ngx.var.server_name,ngx.var.server_addr,ngx.var.request_method,ngx.var.scheme,ngx.var.host,ngx.var.request_uri,ngx.var.status,ngx.var.referer,handle_body(ngx.var.request_body),ngx.var.http_user_agent)
60+
61+
if(ngx.req.get_method() == 'POST')
62+
then
63+
write_content(file_name, extend..'\n')
64+
end
65+
```
66+
#### 6.Lua加载顺序
67+
```
68+
![avatar](https://cloud.githubusercontent.com/assets/2137369/15272097/77d1c09e-1a37-11e6-97ef-d9767035fc3e.png)
69+
```

0 commit comments

Comments
 (0)