nginx日誌

 

nginx日誌
    1、錯誤日誌:主要記錄客戶端訪問nginx出錯時的日誌,格式不支持自定義,
        
       如何關閉:  http 外定義 error_log  /dev/null; #注意 這樣寫並不會關閉,而是寫入到off的文件裏 error_log off;
       默認存放位置:  <prerix>/logs/
                        <prerix>/logs/nginx.pid
       
     2、訪問日誌 記錄用戶的訪問信息; 能夠單獨設置到某個location裏面 
     
        Syntax:    access_log path [ format [ buffer = size [ flush = time ]]]
        access_log path format gzip[= [ buffer = size ] [ flush = time ]
        access_log off
        Default:    logs/access.log combined
        Context:    http
                    server
                    location #如記錄頁面404信息
                    if in location
                    limit_except
        Reference:    access_log
        
        默認值: access_log logs/access.log combined;
        如:access_log    /home/v_jksong/log/nginx_access_log.txt mylog;
    
        #自定義日誌的格式:
        log_format
        Syntax:    log_format name string ...
        Default:    combined "..." #默認
        Context:     http
        Reference:    log_format
        
        默認的 combined
        log_format  combined  '$remote_addr - $remote_user  [$time_local]  '
                                   ' "$request"  $status  $body_bytes_sent  '
                                   ' "$http_referer"  "$http_user_agent" ';
        
        #日誌格式容許包含的變量註釋以下:
            $remote_addr, $http_x_forwarded_for 記錄客戶端IP地址
            $remote_user 記錄客戶端用戶名稱
            $request 記錄請求的URL和HTTP協議
            $status 記錄請求狀態
            $body_bytes_sent 發送給客戶端的字節數,不包括響應頭的大小; 該變量與Apache模塊mod_log_config裏的「%B」參數兼容。
            $bytes_sent 發送給客戶端的總字節數。
            $connection 鏈接的序列號。
            $connection_requests 當前經過一個鏈接得到的請求數量。
            $msec 日誌寫入時間。單位爲秒,精度是毫秒。
            $pipe 若是請求是經過HTTP流水線(pipelined)發送,pipe值爲「p」,不然爲「.」。
            $http_referer 記錄從哪一個頁面連接訪問過來的
            $http_user_agent 記錄客戶端瀏覽器相關信息
            $request_length 請求的長度(包括請求行,請求頭和請求正文)。
            $request_time 請求處理時間,單位爲秒,精度毫秒; 從讀入客戶端的第一個字節開始,直到把最後一個字符發送給客戶端後進行日誌寫入爲止。
            $time_iso8601 ISO8601標準格式下的本地時間。
            $time_local 通用日誌格式下的本地時間。

            #參考實例
            http {
                log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                                                    '"$status" $body_bytes_sent "$http_referer" '
                                                    '"$http_user_agent" "$http_x_forwarded_for" '
                                                    '"$gzip_ratio" $request_time $bytes_sent $request_length';

                log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
                                            '"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
                                            '[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';

                open_log_file_cache max=1000 inactive=60s;

                server {
                    server_name ~^(www\.)?(.+)$;
                    access_log logs/$2-access.log main;
                    error_log logs/$2-error.log;

                    location /srcache {
                        access_log logs/access-srcache.log srcache_log;
                    }
                }
            }
    
    
    
    參考文章:http://www.ttlsa.com/linux/the-nginx-log-configuration/
相關文章
相關標籤/搜索