#使用的用戶和組 #user nobody; #指定工做衍生的進程數量(通常爲CPU總核數,或是總核數的兩倍) worker_processes 4; #指定錯誤日誌存放路徑,錯誤日誌的可選級別(debug|info|notice|warn|error|crit) #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #指定pid的存放路徑 #pid logs/nginx.pid; events { #使用的網絡I/O模型,Linux用epoll模型,FreeBSD系統採用kqueue模型 use epoll; #容許worker的鏈接數 worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #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 access ' [$time_local] | $host | $remote_addr | $request | $request_time | $body_bytes_sent | $status |' '| $upstream_addr | $upstream_response_time | $upstream_status |' ' "$http_referer" | "$http_user_agent" '; access_log logs/access.log access; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #響應數據的來源 upstream tomcats { server xupan001:8888 weight=1; server xupan002:8888 weight=1; server xupan003:8888 weight=1; } server { listen 80; server_name xupan003; location ~ .* { proxy_pass http://tomcats; } } }
main爲日誌格式化名字
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';css
$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址;
$remote_user :用來記錄客戶端用戶名稱;
$time_local : 用來記錄訪問時間與時區;
$request : 用來記錄請求的url與http協議;
$status : 用來記錄請求狀態;成功是200,
$body_bytes_sent :記錄發送給客戶端文件主體內容大小;
$request_body:請求體
$http_referer :用來記錄從哪一個頁面連接訪問過來,上一個鏈接地址
$http_user_agent :記錄客戶瀏覽器的相關信息;java
日誌文件存儲日誌路徑:安裝目錄下的logs文件夾下的access.log,main爲日誌格式化名字
access_log logs/access.log mainnginx
若是不須要日誌配置
access_log offshell
[03/Jan/2018:15:49:46 +0800] | xupan003 | 192.168.0.104 | GET /user_info HTTP/1.1 | 0.012 | 124 | 404 || 192.168.0.118:8888 | 0.012 | 404 | "http://xupan003/" | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
[03/Jan/2018:15:49:46 +0800] | xupan003 | 192.168.0.104 | GET /static/h-ui.admin/skin/black/icon_arrow.png HTTP/1.1 | 0.009 | 0 | 304 || 192.168.0.119:8888 | 0.009 | 304 | "http://xupan003/static/h-ui.admin/skin/black/skin.css" | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
[03/Jan/2018:15:49:46 +0800] | xupan003 | 192.168.0.104 | GET /favicon.ico HTTP/1.1 | 0.014 | 1162 | 200 || 192.168.0.120:8888 | 0.014 | 200 | "http://xupan003/" | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" 瀏覽器
配置shell腳本:splitLogsByDate.shtomcat
#!/bin/bashbash
# 設置日誌文件存放目錄
logs_path="/usr/local/nginx/logs/"網絡
# 設置pid文件
pid_path="/usr/local/nginx/pid/nginx.pid"app
# 重命名日誌文件
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log負載均衡
# 向nginx主進程發信號從新打開日誌
kill -USR1 `cat ${pid_path}`
crontab中設置定時做業
進行編輯
crontab -e
配置內容以下
0 0 * * * bash /usr/local/nginx/nginx_log.sh 這樣在天天的夜晚12點就會自動建立備份文件了。.