1. 程式人生 > 實用技巧 >nginx 日誌管理配置詳解

nginx 日誌管理配置詳解


nginx的日誌管理


log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

main			# 日誌格式的名字    
$remote_addr		# 訪問主機的ip
$remote_user		# 訪問使用者的憑證,網頁沒有設定就沒有。
$time_local			# 伺服器本地時間
$request			# 請求和http版本
$status				# 請求的狀態碼
$body_bytes_sent	# 伺服器返回的大小
$http_referer		# 這個請求是從那個網站訪問過來的
$http_user_agent	# 能看到客戶端的瀏覽器和作業系統資訊
$http_x_forwarded_for	 # 源客戶端IP,經過代理訪問,記錄原來的IP地址


日誌的呼叫


在寫好日誌格式和取好名稱之後直接使用下面方式呼叫。

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  zidingyi  '訪問的IP:$remote_addr - 認證的使用者:$remote_user - 訪問的時間:[$time_local] - 請求
的資源:$request - 狀態碼:$status - 傳送資料量:$body_bytes_sent - http_refere:"$http_referer"';

    # access_log  /var/log/nginx/access.log  main;
    access_log  /var/log/nginx/zidingyi_access.log  zidingyi;

區域性的呼叫方式。

[root@web01 /etc/nginx]# vi nginx.conf
............
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  zidingyi  '訪問的IP:$remote_addr - 認證的使用者:$remote_user - 訪問的時間:[$time_local] - 請求
的資源:$request - 狀態碼:$status - 傳送資料量:$body_bytes_sent - http_refere:"$http_referer"';
access_log  /var/log/nginx/access.log  main;

................
# =================================================================#
[root@web01 /etc/nginx/conf.d]# vi web01.conf 
server {
        listen 80;
        server_name 10.0.0.7;

        location / {
                root /code/web01;
                index index.html;
}
        access_log  /var/log/nginx/web01_access.log  zidingyi;   
}


日誌的路徑


用指定路徑會在指定的目錄下生成日誌檔案。

# 這種配置方法會在指定目錄下生成日誌。/code/
[root@web01 /etc/nginx]# vi conf.d/web01.conf 
server {
        listen 80;
        server_name 10.0.0.7;

        location / {
                root /code/web01;
                index index.html;
}
        access_log  /code/web01_zi_access.log  main;
}

# 會找到/etc/nginx下的log目錄再儲存日誌。如果沒有需要新建目錄。
[root@web01 /etc/nginx]# vi conf.d/web01.conf 
server {
        listen 80;
        server_name 10.0.0.7;

        location / {
                root /code/web01;
                index index.html;
}
        access_log  log/web01_zi_access.log  main; 
}



FBI WARNING

QQ:1402122292 認準原創sheldon 別人叫我曉東