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

简介:

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











本文转自 iekegz 51CTO博客,原文链接:http://blog.51cto.com/jacksoner/1981934,如需转载请自行联系原作者
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
26天前
|
存储 JSON 应用服务中间件
Higress的日志收集中,底层用的是Envoy,可不可以实现类似NGINX的rsyslog发送?
【2月更文挑战第30天】Higress的日志收集中,底层用的是Envoy,可不可以实现类似NGINX的rsyslog发送?
15 2
|
8天前
|
运维 监控 应用服务中间件
LNMP详解(十四)——Nginx日志详解
LNMP详解(十四)——Nginx日志详解
16 2
|
18天前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
26 3
|
1月前
|
域名解析 网络协议 应用服务中间件
nginx-ingress通过ipv6暴露服务,并在nginx ingress日志中记录客户端真实ipv6的ip地址
本文主要通过阿里云提供的clb和nlb来实现,建议是提前创建好双栈的vpc和vsw(使用clb可以不用双栈vpc和vsw)
175 1
|
3月前
|
负载均衡 应用服务中间件 nginx
【实践】使用Nginx作为GrayLog日志接入的负载均衡
【实践】使用Nginx作为GrayLog日志接入的负载均衡
36 0
|
3月前
|
监控 安全 前端开发
Nginx 访问日志中有 Get 别的网站的请求是什么原因?
Nginx 访问日志中有 Get 别的网站的请求是什么原因?
33 0
|
14天前
|
Java
使用Java代码打印log日志
使用Java代码打印log日志
69 1
|
15天前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
70 1
|
19天前
|
SQL 关系型数据库 MySQL
MySQL数据库,可以使用二进制日志(binary log)进行时间点恢复
对于MySQL数据库,可以使用二进制日志(binary log)进行时间点恢复。二进制日志是MySQL中记录所有数据库更改操作的日志文件。要进行时间点恢复,您需要执行以下步骤: 1. 确保MySQL配置文件中启用了二进制日志功能。在配置文件(通常是my.cnf或my.ini)中找到以下行,并确保没有被注释掉: Copy code log_bin = /path/to/binary/log/file 2. 在需要进行恢复的时间点之前创建一个数据库备份。这将作为恢复的基准。 3. 找到您要恢复到的时间点的二进制日志文件和位置。可以通过执行以下命令来查看当前的二进制日志文件和位
|
25天前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
45 0