[root@iZbp1e0xboek6oow616aoiZ ~]# vim /usr/local/nginx/conf/nginx.conf
#注意combined_realip这个自定义的名字
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
[root@iZbp1e0xboek6oow616aoiZ ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
#增加下面一行,注意最后的名字
access_log /tmp/1.log combined_realip;
[root@iZbp1e0xboek6oow616aoiZ ~]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 25 Apr 2018 07:30:31 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Tue, 24 Apr 2018 09:10:29 GMT
Connection: keep-alive
ETag: "5adef485-f"
Accept-Ranges: bytes
[root@iZbp1e0xboek6oow616aoiZ ~]# cat /tmp/1.log
127.0.0.1 - [25/Apr/2018:15:30:31 +0800] test.com "/" 200 "-" "curl/7.29.0"
127.0.0.1 - [25/Apr/2018:15:30:44 +0800] test.com "/" 200 "-" "curl/7.29.0"
Nginx日志切割
[root@iZbp1e0xboek6oow616aoiZ ~]# vim /usr/local/sbin/nginx_log_rotate.sh
#! /bin/bash
## 假设nginx的日志存放路径为/tmp/
d=`date -d "-1 day" +%Y%m%d`
logdir="/tmp/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`
[root@iZbp1e0xboek6oow616aoiZ ~]# vim /usr/local/sbin/nginx_log_rotate.sh
[root@iZbp1e0xboek6oow616aoiZ ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180424
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls 1.log
+ for log in '`ls *.log`'
+ mv 1.log 1.log-20180424
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 4863
[root@iZbp1e0xboek6oow616aoiZ ~]# ls /tmp/
1.log mysql.sock systemd-private-419ed14bb7b74af6acb2464ad878b39e-ntpd.service-1fa9lg
1.log-20180424 pear
Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)> php-fcgi.sock
[root@iZbp1e0xboek6oow616aoiZ ~]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm -f (日志清理)
[root@iZbp1e0xboek6oow616aoiZ ~]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
静态文件不记录日志和过期时间
[root@iZbp1e0xboek6oow616aoiZ ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
[root@iZbp1e0xboek6oow616aoiZ ~]# /usr/local/nginx/sbin/nginx -s reload
[root@iZbp1e0xboek6oow616aoiZ ~]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 25 Apr 2018 11:38:14 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Tue, 24 Apr 2018 09:10:29 GMT
Connection: keep-alive
ETag: "5adef485-f"
Accept-Ranges: bytes
[root@iZbp1e0xboek6oow616aoiZ tmp]# more /tmp/1.log
127.0.0.1 - [25/Apr/2018:19:38:14 +0800] test.com "/" 200 "-" "curl/7.29.0"
[root@iZbp1e0xboek6oow616aoiZ tmp]# vim /data/wwwroot/test.com/123.js
[root@iZbp1e0xboek6oow616aoiZ tmp]# curl -x127.0.0.1:80 test.com/123.js -I
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Wed, 25 Apr 2018 11:40:56 GMT
Content-Type: application/javascript
Content-Length: 0
Last-Modified: Wed, 25 Apr 2018 11:40:50 GMT
Connection: keep-alive
ETag: "5ae06942-0"
Expires: Wed, 25 Apr 2018 23:40:56 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
[root@iZbp1e0xboek6oow616aoiZ tmp]# cat /tmp/1.log
127.0.0.1 - [25/Apr/2018:19:38:14 +0800] test.com "/" 200 "-" "curl/7.29.0"