一、為nginx配置錯誤日志
Nginx錯誤日志是調試nginx的重要手段,屬于核心功能子產品的參數(ngx_core_module)該參數名字為err_log,是放在Main區塊中全局配置
err_log的文法格式以及參數文法說明如下
err_log file level(級别)
關鍵字 日志檔案 錯誤日志級别
其中關鍵字err_log不能改變
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuczMwAjN2ATN20yMyQjNxEzM3EDNwMDM3EDMy0yMyYDO3ATMvw1MwcTMwIzLcNjM2gzNwEzLcd2bsJ2Lc12bj5ycn9Gbi52YuUTMwIzcldWYtl2Lc9CX6MHc0RHaiojIsJye.png)
1 1、在配置檔案中寫入error_log logs/error.log error
2 [[email protected] conf]# Vim nginx.conf
3 worker_processes 1;
4 error_log logs/error.log error; →這裡是寫入的預設log
5 events {
6 worker_connections 1024;
7 }
8 http {
9 include mime.types;
10 default_type application/octet-stream;
11 sendfile on;
12 keepalive_timeout 65;
13 #nginx vhosts config
14 include extra/www.conf;
15 include extra/bbs.conf;
16 include extra/blog.conf;
17 include extra/status.conf;
18 }
19 2、檢查文法,并且重新開機
20 3、檢視日志,如果日志太多可以清空日志然後檢視
21 [[email protected] conf]# cat ../error.log →檢視nginx的錯誤日志檔案
22
23 [[email protected] conf]# > ../logs/error.log →這個是日志太多然後看不清的時候清空日志
24 [[email protected] conf]# cat ../logs/error.log →之後檢視日志
二、通路日志作用以及配置
所有的web軟體都有這樣的功能,會把每個使用者通路網站的日志資訊記錄到指定的日志檔案裡面,供網站提供這分析使用者浏覽行為等,如百度。此功能由nginx_http_log_module子產品負責,對應的官方位址:
http://nginx.org/en/docs/http/ngx_http_log_module.html
Nginx 通路日志參數
log_format 用來定義記錄日志的格式(可以定義多種日志格式,取不同名字即可)
access_log 用來指定日志檔案的路徑及使用的何種日志格式記錄日志
用法如下:
1、取出日志參數格式
下面的log_format格式,在vim nginx_default裡面21到23行
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
下面是定義參數
$http_x_forwarded_for 代理接收用戶端資訊
2、把參數放到配置的nginx.conf裡面
Vim nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' →這裡放參數
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; →放參數
#nginx vhosts config
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/status.conf;
}
配置通路日志如下:
預設配置:access_log logs/access.log cimbined;
放置位置:http,server,location,if in location limit_except
access_log 通路位置,這裡一般在虛拟主機裡面
logs/access.log 記錄檔案
cimbined; 以什麼樣的格式記錄
1、 寫到虛拟主機裡面,這裡先寫到www.conf裡面
[[email protected] conf]# vim extra/www.conf
server {
listen 80;
server_name www.cnblog.co pyrene;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main; -→放到location外面,logs是安裝目錄,後面是名字,以main的方式,這樣才你能和log_format 結合使用
}
2、 檢查文法/application/nginx/sbin/nginx –t,重新開機
3、 檢視日志變化用 tail -f /application/nginx/logs/access_www.log 這裡的是上面起名字防止的位置
1、 浏覽器通路綁定的ip位址就會出現如下:并且和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"';
裡面有參數referer,可以顯示你從那個頁面過來的是什麼手機,什麼系統等等
真實的格式
192.168.197.1 - - [17/Dec/2
016:06:42:14 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/5.0.2.200 Chrome/47.0.2526.73 Safari/537.36" "-"
詳細說明資訊:
上面的還可以優化,如下
三、日志輪詢
nginx中的日志輪詢切割一般用cron+腳本
方法如下:
1、首先寫入腳本
[[email protected] logs]# mkdir /server/scripts -p
[[email protected] logs]# cd /server/scripts/
寫腳本之前先建立腳本存放目錄
[[email protected] scripts]# vim cut_nginx_log.sh
#!/bin/bash
cd /application/nginx/logs
#這裡如果有部落格的log那就把log在這裡mv
/bin/mv access_www.log www_access_$(date +%F -d '-1day').log
/application/nginx/sbin/nginx -s reload
#下面可以把這個推送到備份伺服器然後删除七天之前的
2、執行 /bin/sh /server/scripts/cut_nginx_log.sh
3、檢視是否生成log
[[email protected] logs]# ls
access.log access_www.log error.log nginx.pid www_access_2017-03-03.log
4、定時任務
[[email protected] logs]# crontab -l
#time sync by pyrene 2017-1-16
*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
# nginx acess_log
00 * * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
轉載于:https://www.cnblogs.com/pyrene/p/6502132.html