如何在Nginx的access日志中记录POST、Header和Cookie中的参数信息?

已邀请:

zkbhj - 凯冰科技站长

赞同来自:

记录POST数据:
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;
}
注意:只有在用了proxy_pass或者fastcgi_pass标记的location{ }里面变量$request_body才会生效。 
如果你想记录某个cookie或者header里面的值的话,可以用下面的方法:
$cookie_[COOKIE_NAME]
$http_[HEADER_NAME]
另外,常规的变量有:
$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
官方文档:http://nginx.org/en/docs/http/ ... ction

要回复问题请先登录注册