且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-01 02:21:13

Nginx访问日志

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

    ' $host "$request_uri" $status'

    ' "$http_referer" "$http_user_agent"';

combined_realip:日志名称

其余为日志内容

$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@centos7 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf


server

{

    listen 80;

    server_name test.com  test2.com test3.com;

    index index.html index.htm index.php;

    access_log /tmp/test.com.log combined_realip;


验证:

[root@centos7 ~]# tail /tmp/test.com.log 

127.0.0.1 - [10/Nov/2017:17:48:20 +0800] test.com "/" 401 "-" "curl/7.29.0"

127.0.0.1 - [10/Nov/2017:17:48:30 +0800] test.com "/index.php" 200 "-" "curl/7.29.0"


Nginx日志切割

日志切割脚本

[root@centos7 shell]# vi nginx_log_rotate.sh


#! /bin/bash

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

#定义切割时间(切割一天前的日志)

logdir="/tmp/"

#此处指定要切割的日志路径(该路径来自虚拟主机配置文件)

nginx_pid="/usr/local/nginx/logs/nginx.pid"

#调用pid的目的是执行命令:/bin/kill -HUP `cat $nginx_pid`

#该命令等价于命令:nginx -s reload(重新加载文件)

cd $logdir

for log in `ls *.log`

do

    mv $log $log-$d

done

/bin/kill -HUP `cat $nginx_pid`

#执行此命令进行重载生成新的日志文件,与kill -usr1 一样


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

验证访图片的时候:

[root@centos7 test.com]# curl -x127.0.0.1:80 test.com/baidu.png -I

HTTP/1.1 200 OK

Server: nginx/1.12.1

Date: Fri, 10 Nov 2017 10:01:03 GMT

Content-Type: image/png


[root@centos7 test.com]# curl -x127.0.0.1:80 test.com/index.php

没记录日志

[root@centos7 test.com]# tail /tmp/test.com.log 

127.0.0.1 - [10/Nov/2017:17:48:20 +0800] test.com "/" 401 "-" "curl/7.29.0"

127.0.0.1 - [10/Nov/2017:17:48:30 +0800] test.com "/index.php" 200 "-" "curl/7.29.0"

127.0.0.1 - [10/Nov/2017:18:02:38 +0800] test.com "/index.php" 200 "-" "curl/7.29.0"

127.0.0.1 - [10/Nov/2017:18:02:38 +0800] test.com "/index.php" 200 "-" "curl/7.29.0"


问题:curl访问时间不对,但是系统时间是对的?

[root@centos7 test.com]#  curl -x127.0.0.1:80 test.com/baidu.png -I

HTTP/1.1 200 OK

Server: nginx/1.12.1

Date: Wed, 15 Nov 2017 02:58:15 GMT

Content-Type: image/png

[root@centos7 test.com]# date

Wed Nov 15 10:58:32 CST 2017




'












本文转自方向对了,就不怕路远了!51CTO博客,原文链接:http://blog.51cto.com/jacksoner/1981934 ,如需转载请自行联系原作者