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日志并进行多维度分析。
目录
相关文章
|
1月前
|
存储 JSON 应用服务中间件
Higress的日志收集中,底层用的是Envoy,可不可以实现类似NGINX的rsyslog发送?
【2月更文挑战第30天】Higress的日志收集中,底层用的是Envoy,可不可以实现类似NGINX的rsyslog发送?
19 2
|
1月前
|
前端开发 Java 应用服务中间件
Nginx访问异常的解决方法
Nginx访问异常的解决方法
|
3月前
|
JSON 应用服务中间件 API
利用Grafana的API Key+Nginx反向代理实现Grafana免登录访问
利用Grafana的API Key+Nginx反向代理实现Grafana免登录访问
98 1
|
17天前
|
网络协议 应用服务中间件 Linux
centos7 Nginx Log日志统计分析 常用命令
centos7 Nginx Log日志统计分析 常用命令
33 2
|
18天前
|
运维 监控 应用服务中间件
LNMP详解(十四)——Nginx日志详解
LNMP详解(十四)——Nginx日志详解
16 2
|
19天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
33 0
|
25天前
|
存储 监控 数据可视化
Nginx+Promtail+Loki+Grafana Nginx日志展示
通过这些步骤,你可以将Nginx的日志收集、存储、查询和可视化整合在一起。这样,你就可以在Grafana中轻松地创建和展示Nginx日志的图表和面板。
31 3
|
27天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0
|
28天前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
28 3
|
1月前
|
前端开发 应用服务中间件 nginx
nginx中配置不输入端口(指定地址)访问项目的方法
nginx中配置不输入端口(指定地址)访问项目的方法
27 0