标签:nginx日志 nginx日志切割 nginx设置静态文件过期时间与不记录 nginx设置日志格式
nginx访问日志vim /usr/local/nginx/conf/nginx.conf
中间有一行是定义log的格式
log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
‘ $host "$request_uri" $status‘
‘ "$http_referer" "$http_user_agent"‘;
其中‘‘包围起来的不算一行,只是为了美观才分行显示,分行显示就必须加上‘‘号;
含义:
combined_realip:定义日志格式别名,默认为combined_realip,这里我会改为test001;
$remote_addr:客户端ip也就是出口公网ip;
$http_x_forwarded_for:代理服务器ip;
$time_local:服务器本地时间;
$host:访问主机名(域名);
$request_uri:访问的url地址;
$status:状态码,比如404;
$http_referer:referer;
$http_user_agent:也就是访问的浏览器类型,比如傲游7;
vim /usr/local/nginx/conf/vhost/test.com.conf
增加access_log /tmp/test.com.log combined_realip;
其中combined_realip是定义名称,记得与nginx.conf匹配;
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
curl -x127.0.0.1:80 test.com/1.html -I
访问成功200
查看日志
cat /tmp/test.com.log
127.0.0.1 - [15/Mar/2018:03:13:28 +0800] test.com "/1.html" 200 "-" "curl/7.29.0"
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`
sh -x /usr/local/sbin/nginx_log_rotate.sh
[root@shu-test ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d ‘-1 day‘ +%Y%m%d
+ d=20180314
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls php_errors.log test.com.log
+ for log in ‘`ls *.log`‘
+ mv php_errors.log php_errors.log-20180314
+ for log in ‘`ls *.log`‘
+ mv test.com.log test.com.log-20180314
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1574
[root@shu-test ~]#
crontab -e
写入脚本
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
意思:每天凌晨0点执行脚本/usr/local/sbin/nginx_log_rotate.sh
find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm
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;
}
server
{
listen 80;
server_name test.comtest1.comtest2.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 test;
}
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
nginx访问日志配置+日志切割+不记录静态文件日志+设置静态文件过期时间
标签:nginx日志 nginx日志切割 nginx设置静态文件过期时间与不记录 nginx设置日志格式
原文地址:http://blog.51cto.com/shuzonglu/2087005