四十八、Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间

一、Nginx访问日志

日志格式

# pwd

/usr/local/nginx/conf

[[email protected] conf]# vim nginx.conf

log_format ELA '$remote_addr $http_x_forwarded_for [$time_local]'

' $host "$request_uri" $status'

' "$http_referer" "$http_user_agent"';

解析:

combined_realip:日志格式的名字,可自定义。

“;”:分号,Nginx的配置文件以分号隔开,一段配置结束,用分号隔开,换行只要没加分号就是等于是一行。

$remote addr:客户端IP(公网IP)

可以在百度搜索“IP”,就可以知道自己的公网IP。

$http_x_forwarded_for:代理服务器的IP。

$time_local:服务器本地时间。

$host:访问主机名(域名)。

$request_uri:访问的URL地址。

$status:状态码。

$http_referer:referer。

$http_user_agent:user_agent。

除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加。

# pwd

/usr/local/nginx/conf/vhost

[[email protected] vhost]# vim test.com.conf

server

{

listen 80;

server_name test.com test2.com test3.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 ELA;   //如果不写日志格式就会使用默认的。

此处指定访问日志的位置。

}

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

再curl测试,再查看这个日志,显示301的可以查看到。

二、Nginx日志切割

Nginx没有自带的日志切割工具,需要借助于系统的日志切割工具,或者自己写切割日志的脚本。

自定义shell脚本:

# vim /usr/local/sbin/nginx_logrotate.sh

//以后写的脚本放在/usr/local/sbin下

#! /bin/bash

d=`date -d "-1 day" +%Y%m%d`

logdir="/tmp"    日志的路径地址

nginx_pid="/usr/local/nginx/logs/nginx.pid"  执行pid是为了执行kill那行

cd $logdir     进入log目录

for log in `ls *.log`

do            固定语法,表示开始循环

mv $log  $log-$d   //重命名in后ls出来的所有文件再加上日期。

done

/bin/kill -HUP `cat $nginx_pid`   目的和nginx -s reload是一样的

# date -d "-1 day" +%Y%m%d

20180425                       生成昨天的日期

[[email protected] conf]# date

2018年 04月 26日 星期四 05:06:20 CST

# ls /usr/local/nginx/logs/nginx.pid   pid地址要正确,不然kill那条命令就无法执行了。

举例:# for f in `ls `; do ls -l $f;done

for:循环。for log,以log作为它的变量。

f:文件。

in:在哪个区间里循环,in `ls `在ls里循环。

# sh -x /usr/local/sbin/nginx_logrotate.sh

-x:查看它的执行过程。

++ date -d '-1 day' +%Y%m%d

+ d=20180425

+ 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-20180425

+ for log in '`ls *.log`'

+ mv test.com.log test.com.log-20180425

++ cat /usr/local/nginx/logs/nginx.pid

+ /bin/kill -HUP 1023     //如果不kill -HUP,就没法生成新的文件。

# ls /tmp

mysql.sock  php_errors.log-20180425  systemd-private-71d2d82f636347309eff2c7a3230eb7f-vgauthd.service-7NpGWg   test.com.log

pear        php-fcgi.sock            systemd-private-71d2d82f636347309eff2c7a3230eb7f-vmtoolsd.service-aM4C0h  test.com.log-20180425

之后,每天会生成一个这样的带日期的日志文件。

# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm

//对30天以前的做一个清理,如果没有符合条件的就不会清理。

# crontab -e    加一个任务计划

0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh

三、静态文件不记录日志和过期时间

# pwd

/usr/local/nginx/conf/vhost

[[email protected] vhost]# vim test.com.conf

server

{

listen 80;

server_name test.com test2.com test3.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|jpeg|png|bmp|swf)$

{

expires       7d;

access_log off;

}

location ~ .*\.(js|css)$

{

expires       12h;

access_log off;

}

access_log /tmp/test.com.log ELA;

}

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

再访问这些静态文件时,就不会记录日志。

原文地址:http://blog.51cto.com/13576245/2107889

时间: 2024-12-17 22:25:49

四十八、Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间的相关文章

Nginx访问日志 Nginx日志切割 静态文件不记录日志和过期时间

一.Nginx访问日志#vim /usr/local/nginx/conf/nginx.conf日志格式的名字combined_realip可以自定义,例如将它修改为aming,注意,这里定义为什么,后面引用的时候就定义为什么(nginx配置文件看见分号才是这一行结束)#vim /usr/local/nginx/conf/vhost/test.com.conf //增加如下一行#/usr/local/nginx/sbin/nginx -t#/usr/local/nginx/sbin/nginx

十二周三课 Nginx访问日志、 Nginx日志切割、 静态文件不记录日志和过期时间

Nginx访问日志 Nginx的文件格式存在于主配置文件中./usr/local/nginx/conf/nginx.conf然后搜索log_format找到他的配置文件这样我们就可以进行日志的格式配置了. 我们常用如下配置.$remote_addr客户端IP(公网IP)$http_x_forwarded_for代理服务器的IP$time_local服务器本地时间$host访问主机名(域名)$request_uri访问的url地址$status状态码$http_refererreferer$htt

Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间介绍

Nginx访问日志 1. 进入配置文件 [[email protected] src]# vim /usr/local/nginx/conf/nginx.conf  //搜索log_format 参考更改配置成如下: log_format aming '$remote_addr $http_x_forwarded_for [$time_local]' 如图: 日志格式字段含义如下: combined_realip为日志格式的名字,后面可以调用它. 2.到虚拟主机配置文件中指定访问日志的路径 [[

Nginx的访问日志、Nginx的日志切割、Nginx静态文件不记录日志和过期时间

Nginx的访问日志 Nginx的日志切割 Nginx静态文件不记录日志和过期时间 原文地址:http://blog.51cto.com/13515599/2086909

48.Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间

一.Nginx访问日志 日志格式 vim /usr/local/nginx/conf/nginx.conf //搜索log_format combined_realip //规则名字 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加 vim /usr/local/nginx/conf/vhost/test.com.conf 添加 access_log /tmp/1.log combined_realip; 这里的combined_realip就是在nginx.

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

Nginx访问日志 Nginx访问日志主要有两个参数控制:log_format #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)access_log #用来指定日至文件的路径及使用的何种日志格式记录日志 设置访问日志的格式,打开配置文件vim /usr/local/nginx/conf/nginx.conf 搜索关键字log_format就可以找到日志格式配置行log_format语法格式及参数语法说明如下:$remote_addr客户端IP(公网IP)$http_x_forwa

Nginx访问日志、 Nginx日志切割、静态文件不记录日志和过期时间

1.Nginx访问日志 配制访问日志:默认定义格式: log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';  (这是定义日志引用时的名字:combined_realip,后面的内容,就是需要被引用的)可以理

nginx访问日志、Nginx日志切割、 静态文件不记录日志和过期时间

一:nginx访问日志 日志格式vim /usr/local/nginx/conf/nginx.conf //搜索log_format$remote_addr 客户端ip(公网ip)$http_x_forwarded_for 代理服务器的ip$time_local 服务器本地时间$host 访问主机名(域名)$request_uri 访问的url地址$status 状态码$http_referer referer$http_user_agent user_agent除了在主配置文件nginx.c

LNMP(3) 静态文件不记录日志和过期时间、Nginx防盗链、Nginx访问控制、解析php

                    静态文件不记录日志和过期时间 进入到虚拟主机配置文件 cd /usr/local/nginx/conf/vhost  编辑虚拟主机配置文件:vim test.com.conf  添加内容:   配置如下 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires      7d;    //过期时间 access_log off; } location ~ .*\.(js|css)$ { expires