Nginx访问日志
在主配置文件中查看当前的日志格式
搜索log_format
[root@test-a /]# cd /usr/local/nginx/
[root@test-a nginx]# vim conf/nginx.conf
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
相关字段的含义
字段 | 含义--- | ---$remote_addr | 客户端IP(公网IP)
$http_x_forwarded_for | 代理服务器的IP
$time_local | 服务器本地时间
$host | 访问主机名(域名)
$request_uri | 访问的url地址
$status | 状态码
$http_refer | referer
$http_user_agent | user_agent
给虚拟机定义日志格式
虚拟主机配置文件对应的服务配置增加 access_log /tmp/abc.com.log combined_realip;(这里的combined_realip就是在nginx.conf中定义的日志格式名字);重新加载配置,测试
[root@test-a nginx]# vim conf/vhost/abc.com.conf
[root@test-a nginx]# cat conf/vhost/abc.com.conf
server
{
listen 80;
server_name abc.com ab.com a.com;
index index.html index.htm index.php;
root /data/wwwroot/abc.com;
if ($host != 'abc.com'){
rewrite ^/(.*)$ http://abc.com/$1 permanent;
}
access_log /tmp/abc.com.log combined_realip; #
}
[root@test-a nginx]# ./sbin/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@test-a nginx]# ./sbin/nginx -s reload
[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com
This is abc.com site.
[root@test-a nginx]# vim conf/vhost/abc.com.conf
[root@test-a nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:15:06 +0800] abc.com "/" 200 "-" "curl/7.29.0"
Shell脚本切割日志
Nginx没有自带的日志切割工具,写一个脚本做到同样的功能。
[root@test-a nginx]# vim /usr/local/sbin/nginx_log_rotate.sh
[root@test-a nginx]# cat /usr/local/sbin/nginx_log_rotate.sh
#!/bin/bash
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@test-a nginx]# ls /tmp/
abc.com.log pear php-fcgi.sock web-1
[root@test-a nginx]# sh /usr/local/sbin/nginx_log_rotate.sh
[root@test-a nginx]# ls /tmp/
abc.com.log abc.com.log-20181127 pear php-fcgi.sock web-1
[root@test-a nginx]# date
Wed Nov 28 07:37:54 CST 2018
为脚本设置定时任务,每天零点执行改脚本
[root@test-a nginx]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
静态文件不记录日志及设置过期时间
[root@test-a nginx]# vim conf/vhost/abc.com.conf
[root@test-a nginx]# cat conf/vhost/abc.com.conf
server
{
listen 80;
server_name abc.com ab.com a.com;
index index.html index.htm index.php;
root /data/wwwroot/abc.com;
if ($host != 'abc.com'){
rewrite ^/(.*)$ http://abc.com/$1 permanent;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
access_log /tmp/abc.com.log combined_realip;
}
[root@test-a nginx]# ./sbin/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@test-a nginx]# ./sbin/nginx -s reload
[root@test-a nginx]# cat /tmp/abc.com.log
[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com -I
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Tue, 27 Nov 2018 23:59:05 GMT
Content-Type: text/html
Content-Length: 22
Last-Modified: Mon, 26 Nov 2018 21:36:51 GMT
Connection: keep-alive
ETag: "5bfc6773-16"
Accept-Ranges: bytes
[root@test-a nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0"
[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.jpg -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.1
Date: Tue, 27 Nov 2018 23:59:21 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@test-a nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0"
[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.js -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.1
Date: Wed, 28 Nov 2018 00:00:46 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@test-a nginx]# cat /tmp/abc.com.log
127.0.0.1 - [28/Nov/2018:07:59:05 +0800] abc.com "/" 200 "-" "curl/7.29.0"
[root@test-a nginx]# curl -utest1:test1 -x127.0.0.1:80 abc.com/1.jpg -I # 添加对应的图片资源后,可以看到Cache-Control过期时间
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Wed, 28 Nov 2018 00:03:09 GMT
Content-Type: image/jpeg
Content-Length: 4
Last-Modified: Wed, 28 Nov 2018 00:02:59 GMT
Connection: keep-alive
ETag: "5bfddb33-4"
Expires: Wed, 05 Dec 2018 00:03:09 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes