访客日志

处理日志模块的官网教程

https://nginx.org/en/docs/http/ngx_http_log_module.html

创建nginx访问日志

日志对于程序员很重要,可用于问题排错,记录程序运行状态,一个好的日志能够给与精确的问题定位。

Nginx日志功能需要在nginx.conf中打开相关指令log_format,设置日志格式,以及设置日志的存储位置access_log,指定日志的格式,路径,缓存大小。

1.日志格式字段解释

nginx.conf中有关访客日志定义如下
#a
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; 参数解释
$remote_addr :记录访问网站的客户端IP地址
$remote_user :记录远程客户端用户名称
$time_local :记录访问时间与时区
$request :记录用户的 http 请求起始行信息(请求方法,http协议)
$status :记录 http 状态码,即请求返回的状态,例如 200 、404 、502 等
$body_bytes_sent :记录服务器发送给客户端的响应 body 字节数
$http_referer :记录此次请求是从哪个链接访问过来的,可以根据 referer 进行防盗链设置
$http_user_agent :记录客户端访问信息,如浏览器、手机客户端等
$http_x_forwarded_for :当前端有代理服务器时,设置 Web 节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的 x_forwarded_for 设置 备注
$remote_addr 可能拿到的是反向代理IP地址
$http_x_forwarded_for 可以获取客户端真实IP地址

2.日志格式参考

3.生成日志

1.检测日志
[root@web-9 ~]#tail -f /var/log/nginx/access.log 2.循环访问10次
[root@web-9 ~]#for i in {1..10};do curl 10.0.0.9/yuchao.png;done 3.查看日志

4.关闭日志

若是有大量日志写入是比较占用磁盘IO,特殊情况下,可以关闭日志功能;

关于日志的2个参数
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main;

日志指令语法

access_log  path  [format  buffer=size  | off]

path代表日志存放路径
off是关闭日志

关闭日志记录

#access_log  /var/log/nginx/access.log  main;
access_log off;
此时就不会记录访客日志了

修改nginx访客日志的格式

自己添加,可以捕获更多的客户端的信息

https://nginx.org/en/docs/

https://nginx.org/en/docs/http/ngx_http_core_module.html#var_remote_addr

当你的nginx访客日志,需要记录更多的client请求信息,你可以来这里找,添加更多的变量,加入到如下的日志格式化参数中


log_format main '$document_uri $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
 

单个虚拟主机,记录日志

针对每一个网站,单独的记录日志文件,便于分类管理。

正确的日志使用姿势如下。

·

语法就是,将日志格式的配置参数,别写在http{}花括号中,而是写在各自的server{}虚拟主机中即可。
# 语法要求,log_format 格式化的日志的名字,还不得重复 1.去掉nginx.conf中的日志配置
# http{}区域中 nginx.conf中什么也别写了
# log_format参数依然得写在http{}区域中 ,可以利用include语法实现
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
} 2.针对每一个虚拟主机,添加日志格式参数,主要的是,分别写入到不同的日志文件中
[root@web-8 /etc/nginx/conf.d]#cat dnf.linux0224.conf
# 这个参数和server{}平级 log_format main '$document_uri $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
# 开启日志功能,以及存放路径,参数写在server{}内
access_log /var/log/nginx/dnf.linux0224.cc.access.log main;
listen 80;
server_name dnf.linux0224.cc; # 这里写的是域名
charset utf-8;
location / {
root /www/dnf/;
index index.html;
} } 3. 单独记录lol域名业务的访客日志
[root@web-8 /etc/nginx/conf.d]#cat lol.linux0224.conf log_format main2 '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; server {
access_log /var/log/nginx/lol.linux0224.cc.access.log main2;
listen 80;
server_name lol.linux0224.cc;
charset utf-8;
location / {
root /www/lol/;
index index.html;
} } 4.注意,开启access_log日志的参数,可以写在server{}区域里,但是日志格式化的参数,只能写在http{}区域中

测试不同虚拟主机的日志记录

现在有2个虚拟主机,单独记录了日志

解读日志的写入顺序

看你如何涉及了,你是继续针对每一个虚拟主机,添加日志
遵循上述讲解的语法 1. 给dnf和lol这两个三级域名,子业务的网站,单独记录访客日志 -rw-r--r-- 1 root root 3310 May 19 14:55 dnf.linux0224.cc.access.log -rw-r--r-- 1 root root 2341 May 19 14:55 lol.linux0224.cc.access.log 2. 剩余其他的虚拟主机日志,全部统一记录到 /var/log/nginx/all-server-accesss.log 如下写法,就会去记录,除了你单独指定的虚拟主机的日志,剩下的日志,都会写入到这个all-server-accesss.log 文件中 nginx.conf 主配置如下
# 定义一个全局的设置
http {
include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main3 '$document_uri $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/all-server-accesss.log main3; sendfile on;
#tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;
} # 单独的虚拟主机,单独设置即可。。
到这,看懂扣 1不懂扣 3 # 检测所有的日志
[root@web-8 /var/log/nginx]#tail -f /var/log/nginx/* # nginx的配置,存在虚拟主机的匹配,匹配到谁,就读取谁的配置。 # 只要是ip符合,port符合,并且有优先级加载顺序,就能匹配上 10.0.0.8:80 这个ip 你只要构造符合条件的 请求即可。 第一种虚拟主机,符合 10.0.0.8:80 第二种虚拟主机。符合,修改了端口的 10.0.0.8:81 10.0.0.8:82 第三种虚拟主机,符合修改了ip的 10.0.0.88:80 这里能看懂 扣 6 不懂 7 练习的目的,在于搞懂,你有几个虚拟主机,以及你的请求,与哪一个虚拟主机匹配上了。 # 单独给 lol.linux0224.cc 设置了日志
# 单独给 dnf.linux0224.cc 设置了日志 # 其他的虚拟主机,就会默认匹配 http{ 设置的全局 日志参数了}

如果你觉得,默认的日志,和单独指定的日志,太混乱,

1.你就这么做,每一个虚拟主机,单独的设置日志参数,就行了,不需要去关心那个默认的日志(记录一堆网站的请求,也没什么实际意义)

只有针对单个的网站业务,记录的日志,才有提取,分析的意义。

nginx提供的日志

记录用户访问记录的 ,access_log

记录nginx运行错误的日志  error_log

关于该参数的官网文档,以及具体的用法
https://nginx.org/en/docs/ngx_core_module.html#error_log 和access_log用法一样去以及
http{}
server{} 区域里面 Syntax: error_log file [level];
Default:
error_log logs/error.log error;
Context: main, http, mail, stream, server, location 具体的level是指,日志记录的详细程度
有这些值让你填写
debug, info, notice, warn, error, crit, alert 从左到右,详细程度分别是 从 大 >>> 小 debug 会记录超级详细的信息,没必要,占用大量的磁盘空间
crit 表示nginx以及出现严重错误,以及崩溃了,才记录日志。。记录的内容太少
一般用的,以及默认的就是error日志级别,能够记录基本的,常见错误。
 

5.多虚拟主机的日志

由于企业会有多个网站业务,日志也必然需要分开记录。

单独写入到各自的server{}标签内即可。

1.全局配置

全局定义好日志格式,子页面配置中定义日志路径即可。

[root@web-9 /etc/nginx/conf.d]#cat /etc/nginx/nginx.conf 

user  www;
worker_processes auto; error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid; events {
worker_connections 1024;
} http {
include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; sendfile on;
#tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;
}

2.www页面的日志

定义日志路径

[root@web-9 /etc/nginx/conf.d]#cat www.yuchaoit.conf
server {
listen 80;
server_name www.yuchaoit.cn;
charset utf-8;
access_log /var/log/nginx/www.yuchaoit.log;
location / {
root /usr/share/nginx/html/game;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

3.movie页面日志

[root@web-9 /etc/nginx/conf.d]#cat movie.yuchaoit.conf
server {
listen 80;
server_name movie.yuchaoit.cn;
charset utf-8;
access_log /var/log/nginx/movie.yuchaoit.log;
location / {
root /usr/share/nginx/html/movie;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

4.日志记录结果

三、错误日志

Nginx能够将自身运行故障的信息也写入到指定的日志文件中。对于错误信息的调试,是维护Nginx的重要手段,指令是error_log,可以放在http{}全局中,也可以单独为虚拟主机记录。

语法:
error_log file level; 日志级别在乎debug|info|notice|warn|error|crit|alert|emerg
级别越高,日志记录越少,生产常用模式是warn|error|crit级别
日志的记录,会给服务器增加额外大量的IO消耗,按需修改

0.自动生成配置文件模板

https://www.digitalocean.com/community/tools/nginx?domains.0.php.wordPressRules=true&domains.0.logging.accessLog=true&domains.0.logging.errorLog=true&global.app.lang=zhCN

0.去掉主配置的error_log

[root@web-9 /etc/nginx/conf.d]#cat  /etc/nginx/nginx.conf 

user  www;
worker_processes auto; pid /var/run/nginx.pid; events {
worker_connections 1024;
} http {
include /etc/nginx/mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; sendfile on;
#tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;
}

1.记录movie页面的错误日志

[root@web-9 /etc/nginx/conf.d]#cat movie.yuchaoit.conf
server {
listen 80;
server_name movie.yuchaoit.cn;
charset utf-8;
access_log /var/log/nginx/movie.yuchaoit.log;
error_log /var/log/nginx/error.movie.yuchaoit.log;
location / {
root /usr/share/nginx/html/movie;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

2.记录www页面的错误日志

[root@web-9 /etc/nginx/conf.d]#cat www.yuchaoit.conf
server {
listen 80;
server_name www.yuchaoit.cn;
charset utf-8;
access_log /var/log/nginx/www.yuchaoit.log;
error_log /var/log/nginx/error.www.yuchaoit.log;
location / {
root /usr/share/nginx/html/game;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

3.重启服务,检查日志

[root@web-9 /etc/nginx/conf.d]#systemctl restart nginx

[root@web-9 ~]#ls /var/log/nginx/
access.log error.movie.yuchaoit.log movie.yuchaoit.log
error.log error.www.yuchaoit.log www.yuchaoit.log

4.记录错误日志

当访问该网站出现各种错误时,将被记录日志;

运维可以根据该日志找出当前服务器存在的错误问题,进行修复;

四、404页面优化

nginx指令error_page的作用是当发生错误的时候能够显示一个预定义的uri;

语法1
error_page 404 /404.html; 语法2
error_page 404 https://error.taobao.com/app/tbhome/common/error.html;

1.修改www页面的404优化

默认404页面很丑

优化之后

1.修改配置文件
[root@web-9 /usr/share/nginx/html]#cat /etc/nginx/conf.d/www.yuchaoit.conf
server {
listen 80;
server_name www.yuchaoit.cn;
charset utf-8;
access_log /var/log/nginx/www.yuchaoit.log;
error_log /var/log/nginx/error.www.yuchaoit.log;
error_page 404 /404.html;
location / {
root /usr/share/nginx/html/game;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
} 2.创建404.html,注意这里是表示去访问www.yuchaoit.cn/404.html
因此该文件要放入 root参数指定的网页根目录下
[root@web-9 /usr/share/nginx/html]#cat game/404.html <h1 style='red'>您访问的地址有误,请正确查找 </h1>

2.指定另一个url

server {
listen 80;
server_name movie.yuchaoit.cn;
charset utf-8;
access_log /var/log/nginx/movie.yuchaoit.log;
error_log /var/log/nginx/error.movie.yuchaoit.log;
error_page 404 https://error.taobao.com/app/tbhome/common/error.html;
location / {
root /usr/share/nginx/html/movie;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
~
~

nginx访问日志的更多相关文章

  1. Nginx 访问日志轮询切割

    Nginx 访问日志轮询切割脚本 #!/bin/sh Dateformat=`date +%Y%m%d` Basedir="/application/nginx" Nginxlog ...

  2. 按日期切割nginx访问日志--及性能优化

    先谈下我们需求,一个比较大的nginx访问日志,根据访问日期切割日志,保存在/tmp目录下. 测试机器为腾讯云机子,单核1G内存.测试日志大小80M. 不使用多线程版: #!/usr/bin/env ...

  3. 一、基于hadoop的nginx访问日志分析---解析日志篇

    前一阵子,搭建了ELK日志分析平台,用着挺爽的,再也不用给开发拉各种日志,节省了很多时间. 这篇博文是介绍用python代码实现日志分析的,用MRJob实现hadoop上的mapreduce,可以直接 ...

  4. Python正则表达式,统计分析nginx访问日志

    目标: 1.正则表达式 2.oop编程,统计nginx访问日志中不同IP地址出现的次数并排序 1.正则表达式 #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  5. logstash收集nginx访问日志

    logstash收集nginx访问日志 安装nginx #直接yum安装: [root@elk-node1 ~]# yum install nginx -y 官方文档:http://nginx.org ...

  6. 使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页

    使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页 方法1:linux下使用awk命令 # cat access1.log | awk '{print $1" &q ...

  7. Nginx 访问日志配置

    一.Nginx 访问日志介绍 Nginx 软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由 ngx_http_log_module 模块负责. 二. ...

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

    1.Nginx访问日志 配制访问日志:默认定义格式: log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_loc ...

  9. 采集并分析Nginx访问日志

    日志服务支持通过数据接入向导配置采集Nginx日志,并自动创建索引和Nginx日志仪表盘,帮助您快速采集并分析Nginx日志. 许多个人站长选取了Nginx作为服务器搭建网站,在对网站访问情况进行分析 ...

  10. Logstash+ElasticSearch+Kibana处理nginx访问日志(转)

    ELK似乎是当前最为流行的日志收集-存储-分析的全套解决方案. 去年年初, 公司里已经在用, 当时自己还山寨了一个统计系统(postgresql-echarts, 日志无结构化, json形式存储到p ...

随机推荐

  1. 零信任策略下K8s安全监控最佳实践(K+)

    简介: 本文重点将围绕监控防护展开,逐层递进地介绍如何在复杂的分布式容器化环境中借助可观测性平台,持续监控K8s集群,及时发现异常的 API 访问事件.异常流量.异常配置.异常日志等行为,并且结合合理 ...

  2. 钉钉宜搭入选Forrester《中国低代码平台市场分析报告》

    ​简介:  最新:钉钉宜搭入选Forrester<中国低代码平台市场分析报告>! 11月12日,全球知名研究机构Forrester发布<中国低代码平台市场分析报告(The State ...

  3. WPF 笔迹触摸点收集工具

    本文来安利大家一个工具,可以用来收集笔迹的触摸点,这个工具完全开源 在开始之前先看一下工具的界面 实现方式其实就在触摸的时候收集触摸点信息,上面的工具有很多功能都没有实现的.笔迹绘制的功能使用 WPF ...

  4. aliyun全站DCDN刷新--Django

    1.编写原因: 由于登录到阿里云DCDN,需要登录加打开各种页面,导致推送一次感觉非常麻烦,所以编写(网上以有很多可以借鉴) 2.基础环境 # 所需模块 pip install aliyun-pyth ...

  5. RT-Thread 时钟管理

    一.时钟节拍 任何操作系统都需要提供一个时钟节拍,以供系统处理所有和时间有关的事件,如线程的延时.线程的时间片轮转调度以及定时器超时等.时钟节拍是特定的周期性中断,这个中断可以看做是系统心跳,中断之间 ...

  6. 后端每日一题 2:DNS 解析过程

    本文首发于公众号:腐烂的橘子 本文梗概: DNS 是什么,有什么作用 一条 DNS 记录是什么样的 DNS 域名解析原理 DNS 服务器如何抵御攻击 DNS 是什么,有什么作用 DNS(Domain ...

  7. NASM语法

    NASM汇编语言的语法很简单,由4部分组成: label:instruction operands; comment 这4部分都是可选的.一条语句可以没有label,没有comment,甚至连inst ...

  8. python交教程4:文件操作

    文件操作流程 人类操作一个word流程: 1.找到文件.双击打开 2. 读或修改 3. 保存&关闭 ⽤python操作⽂件也差不多: 只读模式  创建模式  追加模式  遍历文件  图片视频- ...

  9. radmin远程控制软件怎么样,有没有替代品

    Radmin 是流行的.屡获殊荣的安全远程控制软件,它使您能够在远程计算机上实时工作,就像使用它自己的键盘和鼠标一样. 您可以从多个地方远程访问同一台计算机,是网络和管理类别中流行的远程桌面工具. R ...

  10. Django 安全性与防御性编程:如何保护 Django Web 应用

    title: Django 安全性与防御性编程:如何保护 Django Web 应用 date: 2024/5/13 20:26:58 updated: 2024/5/13 20:26:58 cate ...