1. 程式人生 > >Nginx日誌_log_format

Nginx日誌_log_format

Nginx日誌_log_format

1、Nginx日誌型別

(1)error.log-錯誤日誌

(2)access.log-訪問日誌

(3)log_format日誌詳解

 log_format:

Syntax:

log_format name [escape=default|json|none] string ...;

Default:

log_format combined "...";

Context:

http

 

log_format-關鍵字 name-自定義變數

 

[escape=default|json|none] string ...-日誌具體內容,即HTTP請求變數、nginx內建變數、自定義變數組成

 

log_format combined "..."-日誌預設值

 

http-只能配置在http作用域下配置

2、Nginx.conf日誌配置詳解

(1)error_log-關鍵字,即錯誤日誌

/var/log/nginx/error.log-日誌路徑

warn-日誌級別

(2)log_format-關鍵字,即日誌格式

main-自定義變數標識

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

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"'

-日誌內容,即HTTP請求變數、nginx內建變數、自定義變數組成

(3)access.log-關鍵字,即訪問日誌

access.log-關鍵字,即訪問日誌

/var/log/nginx/access.log-日誌路徑

main-日誌格式識別符號,即引用的是log_format中自定義日誌main變數

3、Nginx變數

(1)HTTP請求變數

  • arg_PARAMETER-客戶端請求服務端的get請求引數

        $arg_PARAMETER #這個變數包含GET請求中,如果有變數PARAMETER時的值。

        $args #這個變數等於請求行中(GET請求)的引數,例如foo=123&bar=blahblah;

  • http_HEADER-客戶端請求服務端的http請求頭變數
  • sent_http_HEADER-服務端返回給客戶端的http響應頭變數

(2)內建變數

可以訪問nginx官網檢視:http://nginx.org/en/docs/http/ngx_http_core_module.html

(3)自定義變數

配置$foo=hello

location /test {

            set $foo hello;

            echo "foo: $foo";

    }

輸出

[[email protected] html]# nginx -s reload

[[email protected] html]# curl localhost/test

foo:  hello

4、修改nginx.conf-log_format配置

(1)增加HTTP請求變數演示

增加HTTP請求變數演示:arg_PARAMETER、http_HEADER、sent_http_HEADER配置

'"$arg_wd"-"$args"-"$http_x_requested_with"-"$sent_http_cache_control"-"$http_host"-"$sent_http_last_modified" '

(2)重新載入nginx.conf配置

       nginx -t -c /etc/nginx/nginx.conf

       nginx -s reload -c /etc/nginx/nginx.conf

(3)驗證access_log訪問日誌是否生效

       使用curl -v http://localhost:80?wd=wwl&lover=xq測試

       進入到/var/log/nginx/access.log目錄下訪問

       驗證access_log訪問日誌格式是否生效: