Nginx
1. 日志¶
1.1 介绍¶
nginx多虚拟主机的时候,需要分开虚拟主机的日志。也就是单独给虚拟主机指定日志。
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
日志生成的到Nginx根目录logs/access.log文件,默认使用“main”日志格式,也可以自定义格式
参数 | 说明 |
---|---|
$remote_addr | 客户端的ip地址(代理服务器,显示代理服务ip) |
$remote_user | 用于记录远程客户端的用户名称(一般为“-”) |
$time_local | 用于记录访问时间和时区 |
$request | 用于记录请求的url以及请求方法 |
$status | 响应状态码,例如:200成功、404页面找不到等。 |
$body_bytes_sent | 给客户端发送的文件主体内容字节数 |
$http_user_agent | 用户所使用的代理(一般为浏览器) |
$http_x_forwarded_for | 可以记录客户端IP,通过代理服务器来记录客户端的ip地址 |
$http_referer | 可以记录用户是从哪个链接访问过来的 |
如果不想使用Nginx预定义的格式,可以通过log_format指令来自定义。
nginx自定义日志
log_format name [escape=default|json] string ...; 1.name 格式名称。在access_log指令中引用。 2.escape 设置变量中的字符编码方式是json还是default,默认是default。 3.string 要定义的日志格式内容。该参数可以有多个。参数中可以使用Nginx变量。
1.2 日志调用¶
access_log /tmp/test.com.log main;
2. 案例¶
[root@master extra]# pwd /app/nginx/conf/extra [root@master extra]# ls bbs.conf blog.conf www.conf [root@master extra]# cat bbs.conf blog.conf www.conf server { listen 80; server_name bbs.caimengzhi.org; root html/bbs; index index.html index.htmi index.php; access_log logs/bbs_access.log main; } server { listen 80; server_name blog.caimengzhi.org; root html/blog; index index.html index.htmi index.php; access_log logs/blog_access.log; } server { listen 80; server_name www.caimengzhi.org; root html/www; index index.html index.htmi index.php; access_log logs/www_access.log; } [root@master extra]# cd .. 指定nginx日志格式log_format [root@master conf]# cat nginx.conf error_log logs/error.log error; worker_processes 4; 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"'; include extra/www.conf; include extra/bbs.conf; include extra/blog.conf; #或者直接包含所有 # include extra/*.conf; #表示包含extra所有的.conf文件 } [root@master nginx]# /app/nginx/sbin/nginx -t nginx: the configuration file /app/nginx-1.14.2/conf/nginx.conf syntax is ok nginx: configuration file /app/nginx-1.14.2/conf/nginx.conf test is successful [root@master nginx]# /app/nginx/sbin/nginx -s reload [root@master nginx]# [root@master nginx]# pwd /app/nginx [root@master nginx]# ls logs/ access.log bbs_access.log blog_access.log error.log nginx.pid www_access.log [root@master nginx]# cat logs/www_access.log 127.0.0.1 - - [13/Mar/2019:16:48:33 +0800] "GET / HTTP/1.1" 200 19 "-" "curl/7.29.0"
3. 日志切割¶
nginx的日志文件没有rotate功能。编写每天生成一个日志,我们可以写一个nginx日志切割脚本来自动切割日志文件。
第一步就是重命名日志文件,不用担心重命名后nginx找不到日志文件而丢失日志。在你未重新打开原名字的日志文件前,nginx还是会向你重命名的文件写日志,Linux是靠文件描述符而不是文件名定位文件。
第二步reload nginx服务。
定时脚本cut_nginx_log
.sh
cd /app/nginx/logs/ && \ /bin/mv www_access.log www_access_$(date +%F -d -1day).log /application/nginx/sbin/nginx -s reload
Crontab -e 0 0 * * * /bin/sh /server/script/cut_nginx_log.sh >/dev/null 2>&1