1. 程式人生 > >nginx日誌文件配置

nginx日誌文件配置

editable otto sse ant anti x86_64 客戶端 1.2 基本

nginx日誌文件

錯誤日誌

錯誤日誌級別分為:debug(調試),info(基本信息),notice(基本信息),warn(警告),error(錯誤),crit(更嚴重),默認為crit級別

語法格式:
    Syntax:error_log file [level];
    示例:
    error_log logs/www_error.log error;

日誌訪問日誌

官方鏈接:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
#定義日誌出輸格式
log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                     ‘$status $body_bytes_sent "$http_referer" ‘
                     ‘"$http_user_agent" "$http_x_forwarded_for"‘;
#調用定義格式信息,成生訪問日誌
access_log  logs/access.log  main;

參數說明:
$remote_addr:客戶端訪問的源IP信息
$remote_user:客戶端用戶認證信息
[$time_local]:顯示訪問時間
$request:請求行信息
$status:狀態碼信息
$body_bytes_sent:服務端響應給客戶端的數據大小信息
$http_referer:記錄鏈接到網站的域名信息
$http_user_agent:用戶訪問網站客戶端軟件標識信息
$http_x_forwarded_for:反向代理
示例:
#編寫配置文件,添加訪問日誌
[root@web01 conf]# cat nginx.conf
worker_processes  1;
error_log logs/www_error.log error;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include       extra/www.conf;
    include       extra/bbs.conf;
    include       extra/blog.conf;
    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    access_log  logs/www_access.log  main;
}
#重啟服務之後,測試訪問
[root@web01 conf]# ../sbin/nginx -s reload
[root@web01 conf]# curl bbs.etiantian.org
10.0.0.7 bbs.etiantian.org
#日誌輸出
10.0.0.7 - - [25/Feb/2019:11:58:30 +0800] "GET / HTTP/1.1" 200 27 "-" "curl/7.19.7 
(x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"

因日誌文件數據過大,需進行日誌分割

使用sehll腳本進行日誌分割
[root@web01 scripts]# vim cut_log.sh
#!/bin/bash
data_info=$(date +%F-%H:%M)
mv /application/nginx/logs/www_access.log  /application/nginx/logs/access.log.$data_info

/application/nginx/sbin/nginx -s reload

#設置定時任務
# cut nginx log cron
* */6 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null

nginx日誌文件配置