$remote_addr | 客户端IP(公网IP) |
---|---|
$http_x_forwarded_for | 代理服务器的IP |
$time_local | 服务器本地时间 |
$host | 访问主机名(域名) |
$request_uri | 访问的url地址 |
$status | 状态码 |
$http_referer | referer |
$http_user_agent | user_agent |
[root@hanfeng vhost]# vim /usr/local/nginx/conf/nginx.conf
搜索/log_format 找到以下内容,就是来定义日志格式的
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_referer | referer(跳转页) |
$http_user_agent | user_agent(标识) |
[root@hanfeng vhost]# vim test.com.conf
在第一个括号中添加access_log /tmp/1.log combined_realip;即可
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
access_log /tmp/test.com.log combined_realip;
}
保存退出
[root@hanfeng vhost]# /usr/local/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@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]#
[root@hanfeng vhost]# curl -x127.0.0.1:80 test1.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:15:18 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/
[root@hanfeng vhost]# curl -x127.0.0.1:80 test2.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:15:25 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/
[root@hanfeng vhost]#
[root@hanfeng vhost]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:22:15:18 +0800] test1.com "/" 301 "-" "curl/7.29.0"
127.0.0.1 - [04/Jan/2018:22:15:25 +0800] test2.com "/" 301 "-" "curl/7.29.0"
[root@hanfeng vhost]#
#! /bin/bash
## 假设nginx的日志存放路径为/data/logs/
d=`date -d "-1 day" +%Y%m%d`
logdir="/data/logs"
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@hanfeng vhost]# vim /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`
date -d “-1 day” +%Y%m%d
// 生成昨天的日期,格式为年月日cat $nginx_pid
,而这个命令目的和nginx -s relo 是一样的ls *.log
//开始语句循环,看错有哪些 log后缀的文件cat $nginx_pid
// 重新加载,生成一个新的“nginx_pid=”/usr/local/nginx/logs/nginx.pid”[root@hanfeng vhost]# ls
aaa.com.conf test.com.conf
[root@hanfeng vhost]# for f in `ls `; do ls -l $f ; done
-rw-r--r--. 1 root root 140 1月 3 23:17 aaa.com.conf
-rw-r--r--. 1 root root 295 1月 4 22:07 test.com.conf
[root@hanfeng vhost]#
[root@hanfeng vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180103
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180103
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1612
[root@hanfeng vhost]#
[root@hanfeng vhost]# ls /tmp/
mysql.sock pear php-fcgi.sock test.com.log test.com.log-20180103
[root@hanfeng vhost]#
[root@hanfeng vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
[root@hanfeng vhost]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
date -d “-1 day” +%Y%m%d
”不明白意思[root@hanfneg ~]# date -d “-1 day” +%Y%m%d
20180103
[root@hanfeng vhost]# date
2018年 01月 04日 星期四 23:27:23 CST
cat $nginx_pid
”这个命令也将没有办法继续执行cat $nginx_pid
” z这条命令的意思就是重新加载一次nginx服务cat $nginx_pid
”这条命令的目的是因为切割日志以后 “mv $log $log-$d ” 会将日志移动位置,如果不使用这条命令重新加载一次nginx服务、重新生成一次日志文件,那么将会导致服务出错cat $nginx_pid
”能准确的执行,需要确定nginx的PID所在[root@hanfeng ~]# ls /usr/local/nginx/logs/
access.log error.log nginx_error.log nginx.pid
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ //匹配gif|jpg|jpeg|png|bmp|swf 后缀的文件
{
expires 7d; //7天后过期
access_log off; //匹配“.*.(gif|jpg|jpeg|png|bmp|swf) ”关闭记录日志
}
location ~ .*\.(js|css)$
{
expires 12h; //12个小时后过期
access_log off; //匹配“.*.(js|css) ”关闭记录日志
}
[root@hanfeng vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.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/test.com.log combined_realip;
}
保存退出
[root@hanfeng vhost]# /usr/local/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@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]#
[root@hanfeng vhost]# cd /data/wwwroot/test.com/
[root@hanfeng test.com]# ls
admin index.html
[root@hanfeng test.com]# vim 1.gif 在1.gif随意写入一些内容
[root@hanfeng test.com]# vim 2.js
[root@hanfeng test.com]#
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/1.gif
sdafasf
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/2.js
fghdfsd
[root@hanfeng test.com]# curl -x127.0.0.1:80 test.com/index.html
“test.com”
[root@hanfeng test.com]#
[root@hanfeng test.com]# cat /tmp/test.com.log
127.0.0.1 - [05/Jan/2018:00:17:53 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[root@hanfeng test.com]#
[root@hanfeng test.com]# curl -x127.0.0.1:80 -I test.com/2.js
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 16:22:07 GMT
Content-Type: application/javascript
Content-Length: 8
Last-Modified: Thu, 04 Jan 2018 16:15:42 GMT
Connection: keep-alive
ETag: "5a4e532e-8"
Expires: Fri, 05 Jan 2018 04:22:07 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
[root@hanfeng test.com]#