您的位置:首页 > 运维架构 > Nginx

Nginx日志配置整理

2015-05-16 13:09 661 查看
环境介绍:

OS:CentOS 6.X

Nginx版本:1.4.7以上

打印Nginx日志,对于排查错误必不可缺!

因为Nginx基本都是由模块控制的,参考链接:http://nginx.org/en/docs/http/ngx_http_log_module.html

Nginx日志相关的配置如access_log、error_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log。

这时得赞一下Nginx的日志管理模式:每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令来定义。

1. access_log指令

参考链接:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log

语法: access_log path [format [buffer=size [flush=time]]];

access_log path format gzip[=level] [buffer=size] [flush=time];

access_log syslog:server=address[,parameter=value] [format];

access_log off;

默认值: access_log logs/access.log combined;

配置段: http, server, location, if in location, limit_except

2. error_log指令

参考链接:http://nginx.org/en/docs/ngx_core_module.html#error_log

语法: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];

默认值: error_log logs/error.log error;

配置段: main, http, server, location
配置错误日志。

 默认为crit,可根据需求调整,我指定为error

3. log_format指令

语法: log_format name string …;

默认值: log_format combined “…”;

配置段: http

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

#                  '$status $body_bytes_sent "$http_referer" '

#                  '"$http_user_agent" "$http_x_forwarded_for"';

日志格式允许包含的变量注释如下:

$remote_addr         记录客户端IP地址

$remote_user 记录客户端用户名称

$time_local 通用日志格式下的本地时间。

$request 记录请求的URL和HTTP协议

$status 记录请求状态

$body_bytes_sent 发送给客户端的字节数,不包括响应头的大小;

$http_referer 记录从哪个页面链接访问过来的

$http_user_agent 记录客户端浏览器相关信息
$http_x_forwarded_for 记录客户端IP地址

--先整理这么多。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息