[root@sdwaqw ~]# grep -A2 log_format /usr/local/nginx/conf/nginx.conf
log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
‘ $host "$request_uri" $status‘
‘ "$http_referer" "$http_user_agent"‘;
也可以使用vim打开nginx配置文件,搜索关键字log_format。
变量说明:
combined_realip为日志格式名字,可以改为其他的,后面可以调用它。
[root@sdwaqw ~]# cd /usr/local/nginx/conf/vhost/
[root@sdwaqw vhost]# ls
linuxtest.conf moved.conf zlinux.conf
[root@sdwaqw vhost]# vim linuxtest.conf
server
{
listen 80;
server_name linuxtest.com;
index index.html index.htm index.php;
root /data/wwwroot/linuxtest;
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
access_log /tmp/linuxtest.log combined_realip;
}#使用access_log指定日志存储路径和使用的日志格式名字
[root@sdwaqw vhost]# curl -x127.0.0.1:80 linuxtest.com/ -I
HTTP/1.1 401 UnauthorizedServer: nginx/1.12.2Date: Wed, 14 Mar 2018 07:08:32 GMTContent-Type: text/htmlContent-Length: 195Connection: keep-aliveWWW-Authenticate: Basic realm="Auth"
[root@sdwaqw vhost]# curl -x127.0.0.1:80 -u zlinux:123456 linuxtest.com/ -I
HTTP/1.1 200 OKServer: nginx/1.12.2Date: Wed, 14 Mar 2018 07:08:53 GMTContent-Type: text/htmlContent-Length: 31Last-Modified: Tue, 13 Mar 2018 07:33:35 GMTConnection: keep-aliveETag: "5aa77ecf-1f"Accept-Ranges: bytes
[root@sdwaqw vhost]# curl -x127.0.0.1:80 -u zlinux:123456 linuxtest.com/ -I
HTTP/1.1 200 OKServer: nginx/1.12.2Date: Wed, 14 Mar 2018 07:09:00 GMTContent-Type: text/htmlContent-Length: 31Last-Modified: Tue, 13 Mar 2018 07:33:35 GMTConnection: keep-aliveETag: "5aa77ecf-1f"Accept-Ranges: bytes
[root@sdwaqw vhost]# ls /tmp/linuxtest.log
/tmp/linuxtest.log
[root@sdwaqw vhost]# cat /tmp/linuxtest.log
127.0.0.1 - [14/Mar/2018:15:08:32 +0800] linuxtest.com "/" 401 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:15:08:53 +0800] linuxtest.com "/" 200 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:15:09:00 +0800] linuxtest.com "/" 200 "-" "curl/7.29.0"
Nginx不自带日志切割工具,要想切割Nginx日志需要借助系统的切割工具或者自定义脚本。这里提供一个Nginx日志切割脚本供参考。
[root@sdwaqw vhost]# vim /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash# date +%Y%m%d 显示的是今天的日期 # 加上 -d "-1 day" 显示的是昨天的日期
d=`date -d "-1 day" +%Y%m%d`
#定义日志存放的路径,假设存放在/tmp/下
logdir="/tmp"
# pid文件
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
# 在日志存放路径下循环更改日志文件名 for log in `ls *.log`do
mv $log $log-$ddone
# 在不关闭进程前提下重启,等价于nginx -s reload
/bin/kill -HUP cat $nginx_pid
[root@sdwaqw vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh //-x选项是调试选项
++ date -d ‘-1 day‘ +%Y%m%d
+ d=20180313
+ logdir=/tmp
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp
++ ls linuxtest.log
+ for log in ‘`ls *.log`‘
+ mv linuxtest.log linuxtest.log-20180313
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 14119
[root@sdwaqw vhost]# ls /tmp/ //生成了带日期的日志
linuxtest.log mysql.sock php-fcgi.sock
linuxtest.log-20180313 pear systemd-private-ee3d7331f2804e42bc9bfe8f2b90a0e8-vmtoolsd.service-3B2v7u
[root@sdwaqw vhost]# crontab -e no crontab for root - using an empty one //介入以下内容
0 0 * /bin/bash /usr/local/sbin/nginx_logrotate.sh
[root@sdwaqw vhost]# vim linuxtest.conf
server
{
listen 80;
server_name linuxtest.com;
index index.html index.htm index.php;
root /data/wwwroot/linuxtest;
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
} # ~ 匹配后续的正则表示# 使用\转义.,匹配.jpg等文件
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{# expires设置过期时间
expires 7d;
# 关闭日志记录
access_log off;
}
location ~ .*\.(css|js)$
{
expires 12h;
access_log off;
}
access_log /tmp/linuxtest.log combined_realip;
}
[root@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@sdwaqw vhost]# echo "111111" > /data/wwwroot/linuxtest/test.js
[root@sdwaqw vhost]# echo "111111" > /data/wwwroot/linuxtest/test.jpg
[root@sdwaqw vhost]# echo "111111" > /data/wwwroot/linuxtest/test.ddd //创建一个对比文件,后缀名不在配置列表内
[root@sdwaqw vhost]# curl -x127.0.0.1:80 -u zlinux:1234566 linuxtest.com/test.js -I
HTTP/1.1 200 OKServer: nginx/1.12.2Date: Wed, 14 Mar 2018 07:45:47 GMTContent-Type: application/javascriptContent-Length: 7Last-Modified: Wed, 14 Mar 2018 07:44:42 GMTConnection: keep-aliveETag: "5aa8d2ea-7"Expires: Wed, 14 Mar 2018 19:45:47 GMTCache-Control: max-age=43200 //最大过期时间Accept-Ranges: bytes
[root@sdwaqw vhost]# curl -x127.0.0.1:80 -u zlinux:123456 linuxtest.com/test.jpg -I
HTTP/1.1 200 OKServer: nginx/1.12.2Date: Wed, 14 Mar 2018 07:45:55 GMTContent-Type: image/jpegContent-Length: 7Last-Modified: Wed, 14 Mar 2018 07:45:05 GMTConnection: keep-aliveETag: "5aa8d301-7"Expires: Wed, 21 Mar 2018 07:45:55 GMTCache-Control: max-age=604800 //最大过期时间Accept-Ranges: bytes
[root@sdwaqw vhost]# curl -x127.0.0.1:80 -u zlinux:123456linuxtest.com/test.ddd -I
HTTP/1.1 200 OKServer: nginx/1.12.2Date: Wed, 14 Mar 2018 07:46:01 GMTContent-Type: application/octet-streamContent-Length: 7Last-Modified: Wed, 14 Mar 2018 07:45:29 GMTConnection: keep-aliveETag: "5aa8d319-7"Accept-Ranges: bytes
[root@sdwaqw vhost]# cat /tmp/linuxtest.log //未记录指定后缀日志
127.0.0.1 - [14/Mar/2018:15:42:57 +0800] linuxtest.com "/1.gif" 404 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:15:46:01 +0800] linuxtest.com "/test.ddd" 200 "-" "curl/7.29.0"
91.Nginx配置:访问日志,日志切割,静态文件不记录日志和过期时间
原文地址:http://blog.51cto.com/sdwaqw/2088606