nginx日志配置(cookie,header,post等字段記錄)


 

如果你對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.2/logs/host.access.log /usr/local/Cellar/nginx/1.6.2/logs/host.access.log.2015.4.3
kill -USR1 `cat /usr/local/var/run/nginx.pid`
sleep 1

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM