入职小白
今天带我的大哥给了我一个任务
将服务器的http响应内容记录在日志中!!!
经过一系列的网上查找
可以使用ngx.log()
ngx.log()会将日子写到error.log文件中
所以首先要在配置文件中开error_log 命令
还有设置对应的log等级
具体如下:
error_log logs/error.log debug;
但是,ngx.log()函数只能将日志写入到error.log中,且格式固定,不能随意加入其他变量。
如下:
ngx.log(ngx.INFO, '请求方法:', ngx.var.request_method)
ngx.INFO为等级,后面的为日志内容。
最后记录的内容如下:
2023/07/07 21:11:01 [info] 2536#12044: *194 [lua] getHeader.lua:11: 请求方法:GET, client: 127.0.0.1, server: localhost, request: "GET /lua/getHeader?a=1&b=2&c=3 HTTP/1.1", host: "localhost:9000"
其中,加入的内容是
请求方法:GET,
其他的是不能动的
所以要使用这个方法将响应内容写到日子中,是相当麻烦的。
简单来说,就是
每写一行ngx.say(......)
后面就需要加入一行ngx.lon(ngx.INFO, ......)
这显然不可以这样干啊 太费劲了 大哥既然给了问题 肯定不是靠这样的笨办法实现的。
于是又开始在网上耕耘,经过不懈努力
最后找到了这样一段代码
可以将http响应内容写入到access.log
其中以下内容为log格式定义
log_format mylog escape=json '{ "timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"body_bytes_sent": $body_bytes_sent, '
'"request_time": $request_time, '
'"response_status": $status, '
'"request": "$request", '
'"request_method": "$request_method", '
'"host": "$host",'
'"request_body":"$request_body",'
'"response_body":"$resp_body",'
'"upstream_cache_status": "$upstream_cache_status",'
'"upstream_addr": "$upstream_addr",'
'"http_x_forwarded_for": "$http_x_forwarded_for",'
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent" }';
'"response_body":"$resp_body",' 为对应的响应内容
修改完成保存,会报错,说’$resp_body没有定义
所以还要再下面加上这样一段代码:
lua_need_request_body on;
set $resp_body "";
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
同时,还需要在对应的location内开启日志 access_log logs/access.log mylog;。
location ~ /lua/(.+) {
access_log logs/access.log mylog;
default_type text/html;
charset utf8;
content_by_lua_file lua/$1.lua;
}
整体配置文件如下:
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format mylog escape=json '{ "timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"body_bytes_sent": $body_bytes_sent, '
'"request_time": $request_time, '
'"response_status": $status, '
'"request": "$request", '
'"request_method": "$request_method", '
'"host": "$host",'
'"request_body":"$request_body",'
'"response_body":"$resp_body",'
'"upstream_cache_status": "$upstream_cache_status",'
'"upstream_addr": "$upstream_addr",'
'"http_x_forwarded_for": "$http_x_forwarded_for",'
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent" }';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 9000;
server_name localhost;
resolver 8.8.8.8;
lua_need_request_body on;
set $resp_body "";
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
lua_code_cache off;
location ~ /lua/(.+) {
access_log logs/access.log mylog;
default_type text/html;
charset utf8;
content_by_lua_file lua/$1.lua;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}