nginx 日志默認文件名:
- access.log
- error.log
為了方便查詢及歸檔,將日志文件設置為按照日期來分割,配置如下:
[root@nginx /usr/local/nginx/conf]#cat location.conf
map $time_iso8601 $logdate {
default 'date-not-found';
'~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
}
map
指令是由 ngx_http_map_module
模塊提供的,默認情況下安裝 nginx 都會安裝該模塊。
上面這配置文件使用關鍵字 map
來定義一個變量 $logdate
如果 nginx
內置變量 $time_iso8601
通過正則能匹配到則 獲取到 $logdate = $ymd
否則 $logdate = 'date-not-found'
接下來,在主配置文件中引用:
...
http {
include mime.types;
include location.conf;
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"';
access_log logs/access-$logdate.log main;
...
重載 nginx
[root@nginx /usr/local/nginx/conf]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx /usr/local/nginx/conf]#nginx -s reload
注意:reload后日志文件不會馬上生成,因為日期是通過 $time_iso8601
內置變量獲取的。需要訪問一下才會生成日志文件。
[root@nginx ~]#curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@nginx ~]#ls /usr/local/nginx/logs/
access.log error.log nginx.pid
當訪問觸發以后,如果還沒有生成日期格式的日志文件,則檢查下 nginx 運行用戶及目錄屬主屬組!
[root@nginx ~]#ll /usr/local/nginx/
total 4
drwx------ 2 root root 6 Nov 26 2016 client_body_temp
drwxr-xr-x 2 root root 4096 Jul 30 16:27 conf
drwx------ 2 root root 6 Nov 26 2016 fastcgi_temp
drwxr-xr-x 2 root root 40 Nov 26 2016 html
drwxr-xr-x 2 root root 87 Jul 30 16:20 logs
drwx------ 2 root root 6 Nov 26 2016 proxy_temp
drwxr-xr-x 2 root root 19 Nov 26 2016 sbin
drwx------ 2 root root 6 Nov 26 2016 scgi_temp
drwx------ 2 root root 6 Nov 26 2016 uwsgi_temp
[root@nginx ~]#ps -ef | egrep nginx
root 12916 1 0 16:20 ? 00:00:00 nginx: master process nginx
nginx 12933 12916 0 16:27 ? 00:00:00 nginx: worker process
root 12966 12879 0 17:47 pts/0 00:00:00 grep -E --color=auto nginx
當訪問觸發以后,如果還沒有生成日期格式的日志文件,則檢查下 nginx 運行用戶及目錄屬主屬組!
[root@nginx ~]#chown -R nginx:root /usr/local/nginx/
[root@nginx ~]#ll /usr/local/nginx/
total 4
drwx------ 2 nginx root 6 Nov 26 2016 client_body_temp
drwxr-xr-x 2 nginx root 4096 Jul 30 16:27 conf
drwx------ 2 nginx root 6 Nov 26 2016 fastcgi_temp
drwxr-xr-x 2 nginx root 40 Nov 26 2016 html
drwxr-xr-x 2 nginx root 87 Jul 30 16:20 logs
drwx------ 2 nginx root 6 Nov 26 2016 proxy_temp
drwxr-xr-x 2 nginx root 19 Nov 26 2016 sbin
drwx------ 2 nginx root 6 Nov 26 2016 scgi_temp
drwx------ 2 nginx root 6 Nov 26 2016 uwsgi_temp
# 再次訪問,生成日志文件
[root@nginx ~]#curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@nginx ~]#ll /usr/local/nginx/logs/
total 12
-rw-r--r-- 1 nginx root 270 Jul 30 17:48 access-2020-07-30.log
-rw-r--r-- 1 nginx root 0 Nov 26 2016 access.log
-rw-r--r-- 1 nginx root 412 Jul 30 16:27 error.log
-rw-r--r-- 1 nginx root 6 Jul 30 16:20 nginx.pid