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

Nginx日志格式以及相关配置

2017-06-05 11:35 609 查看
一、Nginx日志格式以及参数说明
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" "$request_body"';

以上配置结果在日志中的显示内容如下:
111.26.32.80 - - [05/Jun/2017:03:07:19 +0800] "GET /css/indexPage.css?v=0.3469464244735898 HTTP/1.1" 200 3117 "http://live.100doc.cn/index?code=0312AIRe0Rf2xx1z22Te0K7CRe02AIRG&state=1" "Mozilla/5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, like Gecko) Mobile/14D27 MicroMessenger/6.5.8 NetType/4G Language/zh_CN"

其中各字段的含义如下:
1、$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
2、$remote_user :等同于用户名,由ngx_http_auth_basic_module认证;
3、$time_local : 用来记录访问时间与时区;
4、$request : 用来记录请求的url与http协议;
5、$status : 用来记录请求状态;成功是200,
6、$body_bytes_s ent :记录发送给客户端文件主体内容大小;
7、$http_referer :用来记录从那个页面链接访问过来的;
8、$http_user_agent :记录客户端浏览器的相关信息;
9、$request_time:
官网描述:request processing time in seconds with a millisecondsresolution; time elapsed between the first bytes were read from theclient and the log write after the last bytes were sent to theclient 。
指的就是从接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出响应数据时间。
10、$upstream_response_time
官网描述:keeps times of responses obtained from upstream servers;times are kept in seconds with a milliseconds resolution. Severalresponse times are separated by commas and colons like addresses inthe $upstream_addr variable
是指从Nginx向后端(php-cgi)建立连接开始到接受完数据然后关闭连接为止的时间。
从上面的描述可以看出,$request_time肯定比$upstream_response_time值大,特别是使用POST方式传递参数时,因为Nginx会把requestbody缓存住,接受完毕后才会把数据一起发给后端。所以如果用户网络较差,或者传递数据较大时,$request_time会比$upstream_response_time大很多。
所以如果使用nginx的accesslog查看php程序中哪些接口比较慢的话,记得在log_format中加入$upstream_response_time。
11、$upstream_addr:后台upstream的地址,即真正提供服务的主机地址
12、当 nginx 尚未读取请求体的时候,或者请求体有一部分或者全部缓冲到临时文件的时候,$request_body 和 $echo_request_body 都将是空值。
Nginx 读取请求体是按需的,如果使用 ngx_proxy 模块的话,读取发生在 content 请求处理阶段。所以如果在早于 content 阶段之前的阶段(比如 rewrite 阶段)去读取 $request_body,则必是空值
处理办法在nginx.conf配置文件中添加了两个配置项:
fastcgi_buffers 32 8k;#指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答。
client_body_buffer_size 1024k; #缓冲区代理缓冲用户端请求的最大字节数
13、$ssl_protocol:SSL协议版本
14、$ssl_cipher:交换数据中的算法

二、Nginx日志配置
1、log_format指令
语法: log_format name string ...;
默认值: log_format combined "...";
配置段: http
name表示格式名称,string表示等义的格式。log_format有一个默认的无需设置的combined日志格式,相当于apache的combined日志格式,如下所示:
log_format combined '$remote_addr - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';
如果nginx位于负载均衡器,squid,nginx反向代理之后,web服务器无法直接获取到客户端真实的IP地址了。 $remote_addr获取反向代理的IP地址。
反向代理服务器在转发请求的http头信息中,可以增加X-Forwarded-For信息,用来记录 客户端IP地址和客户端请求的服务器地址。如下所示:
log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] '
' "$request" $status $body_bytes_sent '
' "$http_referer" "$http_user_agent" ';

2、open_log_file_cache指令
语法: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
默认值: open_log_file_cache off;
配置段: http, server, location
对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off),格式如下:
参数注释如下:
max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive:设置存活时间,默认是10s
min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次
valid:设置检查频率,默认60s
off:禁用缓存
例如:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

3、log_not_found指令
语法: log_not_found on | off;
默认值: log_not_found on;
配置段: http, server, location
是否在error_log中记录不存在的错误。默认是。

4、log_subrequest指令
语法: log_subrequest on | off;
默认值: log_subrequest off;
配置段: http, server, location
是否在access_log中记录子请求的访问日志。默认不记录。

5、rewrite_log指令
由ngx_http_rewrite_module模块提供的。用来记录重写日志的。对于调试重写规则建议开启。 Nginx重写规则指南
语法: rewrite_log on | off;
默认值: rewrite_log off;
配置段: http, server, location, if
启用时将在error log中记录notice级别的重写日志。

6、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
配置错误日志。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: