nginx日志配置(cookie,header,post等字段记录)

时间:2021-10-09 11:33:51

如果你对nginx日志格式,有这样那样的要求。

那么就看一下说明吧。

$remote_addr        The remote host
$remote_user The authenticated user (if any)
$time_local The time of the access
$request The first line of the request
$status The status of the request
$body_bytes_sent The size of the server's response, in bytes
$http_referer The referrer URL, taken from the request's headers
$http_user_agent The user agent, taken from the request's headers

如果你想记录某个cookie或者header里面的值的话

$cookie_[COOKIE_NAME]
$http_[HEADER_NAME]

我们来看下面的测试配置:

http {
include mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user $server_name [$time_local] "$request" '
'$status $body_bytes_sent "$request_body" "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "clickpid=$cookie_clickpid" "clickaid=$cookie_clickaid"'; access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 8080;
server_name localhost; #charset koi8-r; access_log logs/host.access.log main; location ~ \.php$ {
       #这里会记录post数据
access_log logs/post.log main;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1;
} location / {
       #这里不会记录post数据 
root html;
index index.html index.htm;
}

The significance of this variable appears in locations with directives proxy_pass or fastcgi_pass.

只有在用了proxy_pass或者fastcgi_pass标记的location{ }里面变量$request_body才会生效。

可参考:

http://www.cnblogs.com/meteorx/p/3188647.html

另外,还可以参考:

http://articles.slicehost.com/2010/8/27/customizing-nginx-web-logs

日志滚动,可以参考:

http://linux008.blog.51cto.com/2837805/555829/  使用logrotate

或者手动写脚本crontab 来执行:

#!/bin/sh

mv /usr/local/Cellar/nginx/1.6./logs/host.access.log /usr/local/Cellar/nginx/1.6./logs/host.access.log.2015.4.
kill -USR1 `cat /usr/local/var/run/nginx.pid`
sleep

这里mv需要自己判断是否存在重名文件。