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

简介:

Nginx访问日志

  • 日志格式配置

    [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    ......
    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 $http_x_forwarded_for [$time_local]'' $host "$request_uri" $status'' "$http_referer" "$http_user_agent"是日志的内容。

日志内容说明:

名称 含义
$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配置文件中设定日志格式,然后才能在虚拟主机中进行调用(格式名称)

定义test.com.conf的日志格式:
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf 
......
access_log /tmp/test.com.log combined_realip;
......
# 指定日志位置及格式(格式名称)

检测并重新加载配置:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
  • 测试

    访问test2.com:
    [root@localhost ~]# curl -x127.0.0.1 test2.com -I
    curl: (7) Failed connect to 127.0.0.1:1080; 拒绝连接
    [root@localhost ~]# curl -x127.0.0.1:80 test2.com -I
    HTTP/1.1 301 Moved Permanently
    Server: nginx/1.8.0
    Date: Thu, 04 Jan 2018 13:00:43 GMT
    Content-Type: text/html
    Content-Length: 184
    Connection: keep-alive
    Location: http://test.com/

    访问test.com:
    [root@localhost ~]# curl -x127.0.0.1:80 test.com -I
    HTTP/1.1 200 OK
    Server: nginx/1.8.0
    Date: Thu, 04 Jan 2018 13:00:54 GMT
    Content-Type: text/html
    Content-Length: 9
    Last-Modified: Wed, 03 Jan 2018 14:45:43 GMT
    Connection: keep-alive
    ETag: "5a4cec97-9"
    Accept-Ranges: bytes

    查看访问日志:
    [root@localhost ~]# cat /tmp/test.com.log
    127.0.0.1 - [04/Jan/2018:21:00:43 +0800] test2.com "/" 301 "-" "curl/7.29.0"
    127.0.0.1 - [04/Jan/2018:21:00:54 +0800] test.com "/" 200 "-" "curl/7.29.0"

Nginx日志切割

因为Nginx没有自带的日志切割工具,所以需要借助系统日志切割命令或使用日志切割脚本。

  • 日志切割脚本

    为了方便管理,shell脚本统一保存位置:/usr/local/sbin/

    编辑脚本:
    [root@localhost ~]# vim /usr/local/sbin/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(重新加载文件),确保与虚拟主机配置文件变更保持同步
    #该地址来自nginx配置文件
    cd $logdir
    for log in ls *.log
    do
    mv $log $log-$d
    done
    #此处使用通配进行循环,对所有复合条件的日志文件进行切割
    /bin/kill -HUP cat $nginx_pid
    #执行此命令进行重载生成新的日志文件来记录新的日志

    执行脚本:
    [root@localhost ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh 
    ++ date -d '-1 day' +%Y%m%d

    • d=20180103
    • logdir=/tmp/
    • nginx_pid=/usr/local/nginx/logs/nginx.pid
    • cd /tmp/
      ++ ls test.com.log yum.log
    • for log in 'ls *.log'
    • mv test.com.log test.com.log-20180103
    • for log in 'ls *.log'
    • mv yum.log yum.log-20180103
      ++ cat /usr/local/nginx/logs/nginx.pid
    • /bin/kill -HUP 1313
      注意: -x 选项的作用是显示脚本执行过程。该脚本配合任务计划cron使用,定期进行切割和清理。

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

  • 核心配置参数:

    配置虚拟主机配置文件:
    [root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf 
    ......

    添加下面内容到配置文件中

    location ~ ..(gif|jpg|jpeg|png|bmp|swf)$
    #匹配文件类型
    {
    expires 7d;
    #过期时间为7天
    access_log off;
    #不记录该类型文件的访问日志
    }
    location ~ .
    .(js|css)$
    {
    expires 12h;
    #过期时间为12小时
    access_log off;
    #不记录该类型文件的访问日志
    }
    access_log /tmp/test.com.log combined_realip;
    #指定日志位置及格式
    ......

    检测并重载配置文件:
    [root@localhost ~]# /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

  • 测试

    访问test.com:
    [root@localhost ~]# curl -x127.0.0.1:80 test.com
    test.com
    [root@localhost ~]# cat /tmp/test.com.log
    127.0.0.1 - [04/Jan/2018:23:39:40 +0800] test.com "/" 200 "-" "curl/7.29.0"
    #有访问日志

    访问 abc。jpg
    [root@localhost ~]# curl -x127.0.0.1:80 test.com/abc.jpg -I
    HTTP/1.1 200 OK
    Server: nginx/1.8.0
    Date: Thu, 04 Jan 2018 15:41:59 GMT
    Content-Type: image/jpeg
    Content-Length: 247902
    Last-Modified: Thu, 04 Jan 2018 15:41:12 GMT
    Connection: keep-alive
    ETag: "5a4e4b18-3c85e"
    Expires: Thu, 11 Jan 2018 15:41:59 GMT
    Cache-Control: max-age=604800
    Accept-Ranges: bytes

    max-age=604800s=7天,即该文件缓存的过期时间为7天!

    [root@localhost ~]# cat /tmp/test.com.log
    127.0.0.1 - [04/Jan/2018:23:39:40 +0800] test.com "/" 200 "-" "curl/7.29.0"

    返回值为200,访问成功,但无访问日志。


本文转自 豆渣锅 51CTO博客,原文链接:http://blog.51cto.com/754599082/2057592
相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
数据可视化 关系型数据库 MySQL
ELK实现nginx、mysql、http的日志可视化实验
通过本文的步骤,你可以成功配置ELK(Elasticsearch, Logstash, Kibana)来实现nginx、mysql和http日志的可视化。通过Kibana,你可以直观地查看和分析日志数据,从而更好地监控和管理系统。希望这些步骤能帮助你在实际项目中有效地利用ELK来处理日志数据。
900 90
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
878 60
|
安全 应用服务中间件 网络安全
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
885 60
|
应用服务中间件 Linux 网络安全
nginx安装部署ssl证书,同时支持http与https方式访问
为了使HTTP服务支持HTTPS访问,需生成并安装SSL证书,并确保Nginx支持SSL模块。首先,在`/usr/local/nginx`目录下生成RSA密钥、证书申请文件及自签名证书。接着,确认Nginx已安装SSL模块,若未安装则重新编译Nginx加入该模块。最后,编辑`nginx.conf`配置文件,启用并配置HTTPS服务器部分,指定证书路径和监听端口(如20000),保存后重启Nginx完成部署。
4937 8
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
1341 3
|
Web App开发 算法 应用服务中间件
nginx开启局域网https访问
【10月更文挑战第22天】为了调试WebRTC功能,需要在局域网内搭建HTTPS协议。具体步骤包括:在已部署Nginx和安装OpenSSL的环境中生成私钥、证书签名请求和自签名证书;将生成的文件放置到Nginx的证书目录并修改Nginx配置文件,最后重启Nginx服务。注意,自签名证书不受第三方机构认可,如需正式使用,需向CA申请签名。
1110 2
|
应用服务中间件 Shell PHP
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
|
Ubuntu 应用服务中间件 数据库
Nginx配置:阻止非国内IP地址访问的设置方法
此外,出于用户隐私和法律合规性的考虑,应慎重考虑阻止特定国家或地区IP地址的决策。在某些情况下,这可能被视为歧视性或违反当地法律。
1277 3
|
应用服务中间件 nginx
nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
【8月更文挑战第27天】nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
1073 6
|
JavaScript 应用服务中间件 PHP
nginx server 禁止特定目录下的某类文件访问
【8月更文挑战第26天】这段Nginx配置代码旨在保护`/uploads/`目录下的文件,禁止执行任何`.php`, `.html`, `.htm`, 或 `.js`等潜在有害文件,即便被访问也无法运行。取而代之的是重定向到首页。为了实现这一设置,用户需要定位到对应子域名的`.conf`配置文件中进行相应修改。若网站支持多个访问域名,则需确保在正确的`.conf`文件中实施此配置。
532 1