赞
踩
ngxin是一个高性能的HTTP和反向代理web服务器,爱星物联IoT平台也使用了nginx作为反向代理使用。我们在开发和运维过程中,有时候会遇到一些API接口错误、延时、恶意访问等之类的问题,在排查这些问题时,我们依赖详细的日志,试图从各种日志文件中找到一些蛛丝马迹,以便找到问题的根源。所以,本贴重点介绍一下nginx的日志参数介绍、日志配置及日志解析入库操作,日志存到数据库之后,可以作为日志系统的数据源,也可以手动构建SQL去查询统计分析,还可以通过类似grafana之类的工具去查看日志统计及明细。
Nginx访问日志主要有两个参数控制:
log_format #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
access_log #用来指定日至文件的路径及使用的何种日志格式记录日志
lof_format的默认值:
# log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
access_log的默认值:
#access_log logs/access.log main;
log_format语法格式及参数语法说明如下:
log_format <NAME> <Strin¬¬¬g>;
关键字 格式标签 日志格式
关键字:其中关键字error_log不能改变
格式标签:格式标签是给一套日志格式设置一个独特的名字
日志格式:给日志设置格式
log_format格式变量:
$remote_addr #记录访问网站的客户端地址
$remote_user #远程客户端用户名
$time_local #记录访问时间与时区
$request #用户的http请求起始行信息
$status #http状态码,记录请求返回的状态码,例如:200、301、404等
$body_bytes_sent #服务器发送给客户端的响应body字节数
$http_referer #记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置。
$http_user_agent #记录客户端访问信息,例如:浏览器、手机客户端等
$http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置。
access_log语法格式及参数语法说明如下:
access_log <FILE> <NAME>;
关键字 日志文件 格式标签
关键字:其中关键字error_log不能改变
日志文件:可以指定任意存放日志的目录
格式标签:给日志文件套用指定的日志格式
其他语法:
access_log off; #关闭access_log,即不记录访问日志
access_log path [format[buffer=size [flush=time]] [if=condition]];
access_log path formatgzip[=level] [buffer=size] [flush=time] [if=condition];
access_logsyslog:server=address[,parameter=value] [format [if=condition]];
说明:
buffer=size #为存放访问日志的缓冲区大小
flush=time #为缓冲区的日志刷到磁盘的时间
gzip[=level] #表示压缩级别
[if = condition] #表示其他条件
一般场景这些参数都无需配置,极端优化才有可能会考虑这些参数。
lof_format参数的标签段位置:
http
access_log参数的标签段位置:
http, server, location, if in location, limit_except
参考资料:http://nginx.org/en/docs/http/ngx_http_log_module.html
在了解了nginx日志配置项及参数后,我们根据需要进行nginx的日志参数配置。打开nginx的配置文件nginx.conf,定位到http配置块。配置以下信息:
我们创建main日志格式。
http {
...
access_loglogs/access.log main;
log_formatmain '"$time_iso8601" "$connection""$remote_addr" "$request_method" "$request_uri""$server_protocol" $request_length $status $body_bytes_sent $bytes_sent"$http_referer" "$http_user_agent" $request_time$upstream_header_time $upstream_connect_time $upstream_response_time';
...
}
配置好nginx的日志配置后,需要重启nginx服务。
Nginx应用非常广泛,网上有一些大佬写了一些开源的nginx日志解析工具。在爱星物联IoT平台中,我们定制了nginx-clickhouse开源代码,作为nginx日志解析工具。具体代码不再详细介绍,请参考github相关仓库。主要介绍以下配置用法:
该文件配置项如下,注意根据你的环境的事情情况修改下列相关配置项,比如log_path的路径,clickhouse的数据库和表名、IP、端口、用户名、密码等。
settings: interval: 5 log_path: logs/access.log seek_from_end: false clickhouse: db: iot_log table: nginx_access_log host: 127.0.0.1 port: 8123 credentials: user: default password: XXXXXXXX columns: RemoteAddr: remote_addr TimeIso8601: time_iso8601 RequestUri: request_uri Status: status RequestTime: request_time UpstreamHeaderTime: upstream_header_time UpstreamConnectTime: upstream_connect_time UpstreamResponseTime: upstream_response_time BytesSent: bytes_sent BodyBytesSent: body_bytes_sent HttpReferer: http_referer HttpUserAgent: http_user_agent Connection: connection RequestMethod: request_method ServerProtocol: server_protocol RequestLength: request_length nginx: log_type: "main" log_format: '"$time_iso8601" "$connection" "$remote_addr" "$request_method" "$request_uri" "$server_protocol" $request_length $status $body_bytes_sent $bytes_sent "$http_referer" "$http_user_agent" $request_time $upstream_header_time $upstream_connect_time $upstream_response_time'
预先安装好clickhouse数据库服务,创建好日志库,或利用现有库;新建好表格。
爱星物联IoT平台的日志库是iot_log,我们在该库下建好nginx日志表:
CREATE TABLE iot_log.nginx_access_log ( `RemoteAddr` String, `TimeIso8601` DateTime, `RequestUri` String, `Status` UInt16, `RequestTime` Float64, `UpstreamHeaderTime` Float64, `UpstreamConnectTime` Float64, `UpstreamResponseTime` Float64, `BytesSent` UInt64, `BodyBytesSent` UInt64, `HttpReferer` String, `HttpUserAgent` String, `Connection` String, `RequestMethod` String, `ServerProtocol` String, `RequestLength` UInt64 ) ENGINE = Log;
生产环境正常都要设置为开机启动服务,实现开机运行。这里演示介绍,只以后台任务启动的方式运行该程序。
$ nohup ./nginx-clickhouse&
INFO[0000] Reading config file:config/config.yml
INFO[0000] Trying to open logfile:/usr/local/nginx/logs/access.log
以上就是本期分享的内容,目的在于让大家快速适应新品方案的开发,更多资料可从我们官网上获取。
官方官网:https://www.ai-thinker.com
开发资料:https://docs.ai-thinker.com/
官方论坛:http://bbs.ai-thinker.com
技术支持:support@aithinker.com
爱星物联IoT平台体验网址:https://open.iot-aithings.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。