Vor*_*Vor 11 lua nginx http-status-codes
我正在试图弄清楚如何执行以下操作:
请求即将发布.
HttpLuaModule对请求执行一些操作.如果请求有效,则Lua将完成处理ngx.exit(202).但是在处理过程中可能会(并且将会)发生一些条件,并且nginx可能会返回403,404,503错误.
我想要做的是写访问日志只有具有200状态代码的请求.基本上我想做这样的事情:
location /foo {
content_by_lua_file "/opt/nginx/lua/process.lua";
if (status == 200) {
access_log "/path/to/the/access_log"
}
Run Code Online (Sandbox Code Playgroud)
我对nginx和lua都很陌生,所以对我来说,找出放置位置和if语句(以后content_by_lua_file或者在lua文件中的以太)以及if语句应该是什么样子是一个挑战.
Sit*_*hsu 23
nginx 1.7.0+允许在access_log指令本身中使用if条件.
access_log path [format [buffer=size [flush=time]] [if=condition]];
The if parameter (1.7.0) enables conditional logging.
A request will not be logged if the condition evaluates to “0” or an empty string
Run Code Online (Sandbox Code Playgroud)
与map指令相结合,可以根据各种条件将日志事件发送到不同的日志.
http {
map $status $normal {
~^2 1;
default 0;
}
map $status $abnormal {
~^2 0;
default 1;
}
map $remote_addr $islocal {
~^127 1;
default 0;
}
server {
access_log logs/access.log combined if=$normal;
access_log logs/access_abnormal.log combined if=$abnormal;
access_log logs/access_local.log combined if=$islocal;
}
}
Run Code Online (Sandbox Code Playgroud)
http://nginx.org/en/docs/http/ngx_http_log_module.html
http://nginx.org/en/docs/http/ngx_http_map_module.html
这是我想出的解决方案:
-- Some logic goes here
-- ....
-- ....
ngx.var.return_status = 200
Run Code Online (Sandbox Code Playgroud)
http {
lua_package_path .....;
lua_package_cpath ....;
rewrite_by_lua_no_postpone on;
server {
set $return_status 1;
location /foo {
rewrite_by_lua_file "<apth_to_aut.lua";
if ($return_status = 200) {
access_log <path_to_access_log> format;
return 200;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13857 次 |
| 最近记录: |