/ 中存储网

Nginx服务器日志相关配置

2013-12-30 11:02:01 来源:ITJS.CN

网上官网等找了一些文章,配置nginx的日志,英文不好,不敢乱翻译,希望英文好的补充下,现总结整理如下:

一、前言

context:http, server, location

default:access_log log/access.log combined

nginx 日志相关指令主要有两条,一条是log_format,用来设置日志格式,另外一条是access_log,用来指定日志文件的存放路径、格式和缓存大小,通俗的理解就是先用log_format来定义自己想用的日志格式,然后在用access_log定义虚拟主机时或全局日志时在把定义的如下:

log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for';

access_log  /data/logs/access.log  access;

二、几种日志格式配置

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

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

 2、   log_format timing'$remote_addr - $remote_user [$time_local]  $request '

  upstream_response_time $upstream_response_time $proxy_host $proxy_port'

  'msec $msec request_time $request_time';

 3、   log_format up_head '$remote_addr - $remote_user [$time_local]  $request '

  'upstream_http_content_type $upstream_http_content_type';

 4、   log_format mine '$remote_addr - $upstream_addr - $upstream_cache_status - $upstream_status - $upstream_http_host';

access_log  /var/log/nginx/access.log  main;

    access_log /var/log/nginx/timing.log timing;

    access_log /var/log/nginx/up_head.log up_head;

    access_log /var/log/nginx/mine.log mine;

5、指令参数说明

$remote_addr 用户获取客户端的IP地址,但如果前端使用了反向代理,则获取到的是反向代理服务器的IP地址

$http_x_forwarded_for  则可以使得反向代理服务器转发请求的HTTP头信息来记录客户端IP,通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址;$request用于记录请求的URL和HTTP协议。

$time_local 用于记录访问时间与时区

$status 用于记录请求状态,成功时状态为200,页面找到时状态为404

$body_bytes_sent 用于记录发送给客户端的文件主体内容大小

$http_user_agent 用于记录客户端浏览器的相关信息

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

$request  用来记录请求的url与http协议

$remote_user  用来记录客户端用户名称;

$upstream_addr  Address of the upstream server that handled the request

$upstream_status

Upstream server status of the answer

$upstream_response_time

Response time of upstream server(s) in seconds, with an accuracy of milliseconds. Several answers are divided by commas and colons.

$upstream_http_$HEADER

Arbitrary HTTP protocol headers, for example:

$upstream_http_host

$msec he current time at the moment of writing the log entry (microsecond accuracy)

$request_length the length of the body of the request

$connection the number of connection

$bytes_sent the number of bytes transmitted to client

上面指定好了日志格式后就要定义日志存放的路径了,access_log指令用于指定日志存放的路径

如果说不想记录日志,则直接

access_log off;

如果使用如下定义的日志格式存储日志,则可以通过

access_log /var/log/nginx/access.logmain;

表示采用main的日志格式,记录在/var/log/nginx/access.log文件中

(如果把日志存储路径定义在其他地方,要确保Nginx进程设置的用户和组对该目录有读写权限,否则该日志文件就无法生成了)

6、open_log_file_cache

syntax:open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time] | off

default:open_log_file_cache off

context:httpserverlocation

The directive sets the cache, which stores file descriptors of frequently used logs with variable in path.

Directive options:

max - maximal number of descriptors in the cache, with overflow Least Recently Used is removed (LRU); inactive - sets the time after which descriptor without hits during this time are removed; default is 10 seconds; min_uses - sets the minimum number of file usage within the time specified in parameter inactive, after which the file descriptor will be put in the cache; default is 1; valid - sets the time until it will be checked if file still exists under same name; default is 60 seconds; off - disables the cache.

Example:

open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;