简介
Kong如何自定义access_log格式的呢?
Kong的官方文档Logging Reference章节使用了一种很复杂的custom_nginx.template来定义,有没有更简单的方式?
在Configuration Reference章节中,可能通过注入nginx指令,将自定义的格式添加到kong.conf
文件中,任何带有nginxhttp,nginxproxy或nginxadmin前缀的配置,kong在编译后将通过删除前缀而转换为等效的Nginx指令,并添加到Nginx配置的相应部分:
前缀指令 | 说明 |
---|---|
nginxhttp | 注入到整个http块指令中 |
nginxproxy | 注入到kong代理端口的服务模块中 |
nginxadmin | 注入到kong的admin API接口的服务模块中 |
文本格式
配置指令
1、在kong.conf中增加如下配置:
nginx_http_log_format=main '$remote_addr $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" $http_user_agent $http_x_forwarded_for $request_time $upstream_response_time $upstream_addr $upstream_status'
2、在kong.conf中修改proxy_access_log为:
proxy_access_log = logs/access.log main
#重启启动kong
kong restart
最后kong.conf将配置注入到kong_nginx.conf文件
结果如下:
192.168.66.144 - [14/Jan/2021:17:49:27 +0800] "GET /request HTTP/1.1" 401 26 "-" curl/7.29.0 - 0.003 - - -
192.168.66.144 - [14/Jan/2021:17:49:32 +0800] "GET /request HTTP/1.1" 401 26 "-" curl/7.29.0 - 0.003 - - -
json格式
同上在kong.conf中增加如下配置:
nginx_http_log_format=main escape=json '{"clientip":"$remote_addr","timestamp":"$time_iso8601","uri":"$uri","request_uri":"$request_uri","method":"$request_method","status":"$status", "body_bytes_sent":"$body_bytes_sent","http_referer":"$http_referer","http_user_agent":"$http_user_agent","http_x_forwarded_for":"$http_x_forwarded_for","request_time":"$request_time","upstream_response_time":"$upstream_response_time","domain":"$host","upstream_status":"$upstream_status","upstream_addr":"$upstream_addr","clientid":"$http_clientid","platform":"$http_platform","IDFA":"$http_IDFA","http_version":"$http_version","uid":"$http_uid","xcrole":"$http_xcrole","bundle":"$http_bundle","net":"$http_net","ut":"$http_ut","app":"$http_app"}'
然后重启kong网关。
结果如下:
docker模式修改
请参考https://www.jesse.top/2021/01/19/Linux-Web/Kong%E8%87%AA%E5%AE%9A%E4%B9%89%E9%85%8D%E7%BD%AENginx/
编辑修改环境变量env
建议:修改前先备份
建议:修改前先备份
vim ${Docker Root}/containers/${container-id}/config.v2.json
或者
vim ${Docker Root}/containers/${container-id}/config.json
json文件代码是压缩的,可以使用sz path命令下载下来格式化后再编辑,再用rz -y命令上传覆盖
5.重载服务的配置文件
6.重启docker
systemctl daemon-reload
6.重启docker
systemctl restart docker