Nginx核心配置-自定義日志路徑及清空日志注意事項
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.關於日志清空注意事項
1>.nginx服務寫訪問日志是基於access.log的inode寫入的,而非基於文件名稱寫入
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep #首先確認nginx服務是啟動的 root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx nginx 3566 2840 0 10:56 ? 00:00:00 nginx: worker process nginx 3567 2840 0 10:56 ? 00:00:00 nginx: worker process nginx 3568 2840 0 10:56 ? 00:00:00 nginx: worker process nginx 3569 2840 0 10:56 ? 00:00:00 nginx: worker process [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ #請注意觀察access.log文件的inode編號 total 9048 34412586 -rw-r--r-- 1 root root 9231991 Dec 17 10:57 access.log 34412582 -rw-r--r-- 1 nginx nginx 28235 Dec 17 10:57 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# rm -f /yinzhengjie/softwares/nginx/logs/access.log #此處我們將之前的access.log文件刪除 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# touch /yinzhengjie/softwares/nginx/logs/access.log #刪除access.log文件后立即創建一個新的同名文件 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ #文件名稱的確是一樣的,但是我們發現inode編號卻發生了變化 total 32 34553569 -rw-r--r-- 1 root root 0 Dec 17 11:16 access.log 34412582 -rw-r--r-- 1 nginx nginx 28235 Dec 17 10:57 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/ #雖然我們可以刪除和新建了access.log文件,但整個過程並不影響nginx服務的訪問 <h1 style='color:rgb(255,0,255)'>Welcome to 'https://www.cnblogs.com/yinzhengjie/'</h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/login/ <h1 style='color:rgb(255,0,0)'>Java</h1> <h1 style='color:rgb(0,255,0)'>Python</h1> <h1 style='color:rgb(0,0,255)'>Golang</h1> <h1 style='color:rgb(255,0,255)'>Shell</h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ #我們在刪除access.log並創建文件后,對nginx進行了兩次正常訪問,發現日志記錄並沒有記錄,那是因為nginx服務還是記錄之前的access.log的inode編號。 total 32 34553569 -rw-r--r-- 1 root root 0 Dec 17 11:16 access.log 34412582 -rw-r--r-- 1 nginx nginx 28235 Dec 17 10:57 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
2>. 解決nginx服務的access.log無法寫入的問題
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx nginx 3566 2840 0 10:56 ? 00:00:00 nginx: worker process nginx 3567 2840 0 10:56 ? 00:00:00 nginx: worker process nginx 3568 2840 0 10:56 ? 00:00:00 nginx: worker process nginx 3569 2840 0 10:56 ? 00:00:00 nginx: worker process [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/ <h1 style='color:rgb(255,0,255)'>Welcome to 'https://www.cnblogs.com/yinzhengjie/'</h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/login/ <h1 style='color:rgb(255,0,0)'>Java</h1> <h1 style='color:rgb(0,255,0)'>Python</h1> <h1 style='color:rgb(0,0,255)'>Golang</h1> <h1 style='color:rgb(255,0,255)'>Shell</h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ #我們之前使用rm命令刪除並使用touch重新創建了access.log,發現始終無法正常記錄日志了 total 32 34553569 -rw-r--r-- 1 root root 0 Dec 17 11:16 access.log 34412582 -rw-r--r-- 1 nginx nginx 28295 Dec 17 11:23 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# nginx -s reload [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx nginx 3645 2840 3 11:23 ? 00:00:00 nginx: worker process nginx 3646 2840 3 11:23 ? 00:00:00 nginx: worker process nginx 3647 2840 4 11:23 ? 00:00:00 nginx: worker process nginx 3648 2840 3 11:23 ? 00:00:00 nginx: worker process [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/ <h1 style='color:rgb(255,0,255)'>Welcome to 'https://www.cnblogs.com/yinzhengjie/'</h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/login/ <h1 style='color:rgb(255,0,0)'>Java</h1> <h1 style='color:rgb(0,255,0)'>Python</h1> <h1 style='color:rgb(0,0,255)'>Golang</h1> <h1 style='color:rgb(255,0,255)'>Shell</h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ #發現使用"reload"命令行,又可以繼續寫入數據了 total 36 34553569 -rw-r--r-- 1 root root 183 Dec 17 11:23 access.log 34412582 -rw-r--r-- 1 nginx nginx 28295 Dec 17 11:23 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]#
3>.刪除access.log后,使用reload參數可以重寫生成新的access.log(如果刪除了error.log也一樣在重啟nginx時自動創建該文件)
[root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ total 36 -rw-r--r-- 1 root root 183 Dec 17 11:23 access.log -rw-r--r-- 1 nginx nginx 28295 Dec 17 11:23 error.log -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# rm -f /yinzhengjie/softwares/nginx/logs/access.log [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ total 32 -rw-r--r-- 1 nginx nginx 28295 Dec 17 11:23 error.log -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# nginx -s reload [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ total 32 -rw-r--r-- 1 root root 0 Dec 17 11:26 access.log -rw-r--r-- 1 nginx nginx 28355 Dec 17 11:26 error.log -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/login/ <h1 style='color:rgb(255,0,0)'>Java</h1> <h1 style='color:rgb(0,255,0)'>Python</h1> <h1 style='color:rgb(0,0,255)'>Golang</h1> <h1 style='color:rgb(255,0,255)'>Shell</h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ total 36 33691734 -rw-r--r-- 1 root root 95 Dec 17 11:28 access.log 34412582 -rw-r--r-- 1 nginx nginx 28355 Dec 17 11:26 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
4>.生產環境中正確的清空access.log日志的姿勢
[root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ total 36 33691734 -rw-r--r-- 1 root root 95 Dec 17 11:28 access.log 34412582 -rw-r--r-- 1 nginx nginx 28355 Dec 17 11:26 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# echo > /yinzhengjie/softwares/nginx/logs/access.log #使用"echo"命令清空文件可以立即釋放內存空間,若使用rm命令刪除文件並不會立即釋放空間,因為nginx服務還是在引用刪除之前的acess.log的inode編號。 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ total 36 33691734 -rw-r--r-- 1 root root 1 Dec 17 11:31 access.log 34412582 -rw-r--r-- 1 nginx nginx 28355 Dec 17 11:26 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/login/ <h1 style='color:rgb(255,0,0)'>Java</h1> <h1 style='color:rgb(0,255,0)'>Python</h1> <h1 style='color:rgb(0,0,255)'>Golang</h1> <h1 style='color:rgb(255,0,255)'>Shell</h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ total 36 33691734 -rw-r--r-- 1 root root 96 Dec 17 11:31 access.log 34412582 -rw-r--r-- 1 nginx nginx 28355 Dec 17 11:26 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/ <h1 style='color:rgb(255,0,255)'>Welcome to 'https://www.cnblogs.com/yinzhengjie/'</h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -i /yinzhengjie/softwares/nginx/logs/ total 36 33691734 -rw-r--r-- 1 root root 184 Dec 17 11:31 access.log 34412582 -rw-r--r-- 1 nginx nginx 28355 Dec 17 11:26 error.log 34553544 -rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid [root@node101.yinzhengjie.org.cn ~]#
二.自定義訪問日志路徑
1>.編輯主配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-8;
keepalive_timeout 65 65;
#導入其他路徑的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
2>.編輯子配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/auth.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn;
error_page 500 502 503 504 404 /error.html;
access_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_access.log;
error_log /yinzhengjie/softwares/nginx/logs/node101_yinzhengjie_org_cn_error.log;
location / {
root /yinzhengjie/data/web/nginx/html;
index index.html;
}
location /login {
root /yinzhengjie/data/web/nginx;
index index.html;
deny 172.30.1.108;
allow 172.30.1.0/24;
allow 2001:0db8::/32;
deny all;
}
location /error.html {
root /yinzhengjie/data/web/nginx/html/404;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
3>.重新加載nginx服務
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 3715 2840 0 11:36 ? 00:00:00 nginx: worker process
nginx 3716 2840 0 11:36 ? 00:00:00 nginx: worker process
nginx 3717 2840 0 11:36 ? 00:00:00 nginx: worker process
nginx 3718 2840 0 11:36 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 2840 1 0 09:37 ? 00:00:00 nginx: master process nginx
nginx 3791 2840 2 11:56 ? 00:00:00 nginx: worker process
nginx 3792 2840 2 11:56 ? 00:00:00 nginx: worker process
nginx 3793 2840 2 11:56 ? 00:00:00 nginx: worker process
nginx 3794 2840 2 11:56 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
4>.訪問nginx服務,觀察日志大小變化
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/nginx/logs/
total 8
-rw-r--r-- 1 root root 0 Dec 17 11:37 access.log
-rw-r--r-- 1 root root 256 Dec 17 11:56 error.log
-rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid
-rw-r--r-- 1 root root 0 Dec 17 11:53 node101_yinzhengjie_org_cn_access.log
-rw-r--r-- 1 root root 0 Dec 17 11:53 node101_yinzhengjie_org_cn_error.log
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 17 Dec 2019 03:57:26 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 88
Last-Modified: Sun, 15 Dec 2019 15:13:15 GMT
Connection: keep-alive
Keep-Alive: timeout=65
ETag: "5df64d8b-58"
Accept-Ranges: bytes
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/nginx/logs/
total 12
-rw-r--r-- 1 root root 0 Dec 17 11:37 access.log
-rw-r--r-- 1 root root 256 Dec 17 11:56 error.log
-rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid
-rw-r--r-- 1 root root 88 Dec 17 11:57 node101_yinzhengjie_org_cn_access.log
-rw-r--r-- 1 root root 0 Dec 17 11:53 node101_yinzhengjie_org_cn_error.log
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/login
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Tue, 17 Dec 2019 03:57:46 GMT
Content-Type: text/html
Content-Length: 185
Location: http://node101.yinzhengjie.org.cn/login/
Connection: keep-alive
Keep-Alive: timeout=65
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/nginx/logs/
total 12
-rw-r--r-- 1 root root 0 Dec 17 11:37 access.log
-rw-r--r-- 1 root root 256 Dec 17 11:56 error.log
-rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid
-rw-r--r-- 1 root root 378 Dec 17 11:57 node101_yinzhengjie_org_cn_access.log
-rw-r--r-- 1 root root 0 Dec 17 11:53 node101_yinzhengjie_org_cn_error.log
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/login/111111111
HTTP/1.1 404 Not Found
Server: nginx/1.14.2
Date: Tue, 17 Dec 2019 03:58:01 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 1717
Connection: keep-alive
Keep-Alive: timeout=65
ETag: "5df84120-6b5"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/nginx/logs/
total 16
-rw-r--r-- 1 root root 0 Dec 17 11:37 access.log
-rw-r--r-- 1 root root 256 Dec 17 11:56 error.log
-rw-r--r-- 1 root root 5 Dec 17 09:37 nginx.pid
-rw-r--r-- 1 root root 481 Dec 17 11:58 node101_yinzhengjie_org_cn_access.log
-rw-r--r-- 1 root root 272 Dec 17 11:58 node101_yinzhengjie_org_cn_error.log
[root@node101.yinzhengjie.org.cn ~]#