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 別人叫我曉東


免責聲明!

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



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