首页 > 系统相关 >nginx访客日志

nginx访客日志

时间:2024-03-18 18:26:32浏览次数:16  
标签:http log nginx error remote 日志 访客

访客日志

处理日志模块的官网教程
https://nginx.org/en/docs/http/ngx_http_log_module.html

nginx日志变量的格式,作用 找到nginx所有的内置变量,以及作用解释

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

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

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

参数解释 
# 看到这个请求是从什么IP发来的。 remote_addr
$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地址

生产环境下的日志实践经验

开启,关闭日志功能(先看默认的日志功能怎么用)

逐步分析,主配置文件nginx.con

[root@web-8 /etc/nginx]#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"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

检测日志

tail -f /var/log/nginx/access.log


所有的子域名的日志都被统一记录到了一个文件中

关闭日志功能

有时候在代理服务器上,转发服务器上,nginx日志可能不用记录,节省磁盘IO的资源

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"';

    #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个虚拟主机,单独记录了日志

dnf页面的访问情况,与日志记录

那其他的虚拟主机,日志不记录了吗?

解读日志的写入顺序
看你如何涉及了,你是继续针对每一个虚拟主机,添加日志
遵循上述讲解的语法

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

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日志级别,能够记录基本的,常见错误。

错误日志

1. 如何开启error级别的错误日志。
# 单独给的lol虚拟主机网址,设置错误日志,eroor级别 ,配置如下
# 支持写入http{}  server{}
[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;
    error_log /var/log/nginx/lol-error.log error;

    listen 80;
    server_name lol.linux0224.cc;
	charset utf-8;
    location /  {
        root  /www/lol/;
        index  index.html;
    }

}

重启服务
[root@web-8 /etc/nginx/conf.d]#systemctl restart nginx

检查日志
[root@web-8 /etc/nginx/conf.d]#tail -f /var/log/nginx/lol-error.log 

错误日志的,特点是记录,访问时的出错信息

404页面优化,error_page模块

错误页面优化,nginx,默认访问出错后,会返回不同的错误页面

如 -

  • 40x系列的页面
    • 404 not found 服务器上找不到该资源
    • 403 Forbidden 禁止访问(权限不够,找权限的问题)
  • 如50x系列的页面
    • 学nginx反向代理
  • 但是默认的都太丑,对其优化

http://nginx.org/cn/docs/http/ngx_http_core_module.html

语法

# error_page  响应状态码    相对路径的html文件/ 填入url ; 

error_page 404             /404.html;
error_page 500 502 503 504 /50x.html;

优化错误页面,指向本地的html文件

针对lol的虚拟主机设置


[root@web-8 ~]#cat /etc/nginx/conf.d/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"';

# 当404错误时,返回当前机器上的my404.html
# 当403错误时,跳转到淘宝这个错误页面上
server {
    access_log  /var/log/nginx/lol.linux0224.cc.access.log  main2;
    error_log /var/log/nginx/lol-error.log error;
    error_page  404  /my404.html;
    error_page   403   https://error.taobao.com/app/tbhome/common/error.html;


    listen 80;
    server_name lol.linux0224.cc;
	charset utf-8;
    location /  {
        root  /www/lol/;
        index  index.html;
    }

}


# 创建404错误页面 
echo "我是美丽的404错误页面,你访问的资源不存在该服务器上!!!请检查你的URL"  > /www/lol/my404.html


[root@web-8 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#systemctl restart nginx

再次测试错误页面,针对lol这个虚拟主机
404错误页面

403错误的设置
指定跳转到另一个url,

403指定跳转到当前机器的一个文件

[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;
    error_log /var/log/nginx/lol-error.log error;
    error_page  404  /my404.html;
    error_page  403  /my403.html;


    listen 80;
    server_name lol.linux0224.cc;
	charset utf-8;
    location /  {
        root  /www/lol/;
        index  index.html;
    }

}


标签:http,log,nginx,error,remote,日志,访客
From: https://www.cnblogs.com/btcm409181423/p/18081108

相关文章

  • nginx-ingress-controller限制上传文件大小问题
    参考:https://www.cnblogs.com/pitaiyang/p/17975041报错信息nginx-ingress-controller限制上传文件大小为1M如果上传文件大于1M则会在浏览器报以下错误#RequestEntityTooLarge解决方法修改ingress配置文件增加以下配置annotations:#nginx.org/client-max-b......
  • nginx location 和proxy_pass 代理说明
    在nginx中配置proxy_pass的时候,当proxy_pass的最后位置带了/和不带/有很大的区别。当proxy_pass后面的url不带/的时候,相当于直接代理到后端的proxy_pass地址当proxy_pass后面的url带/的时候,相当于代理导当前域名+location路径+后面的访问地址当你使用proxy_pass指令时,如果......
  • 全新架构!日志服务 SLS 自研免登录方案发布
    知乎:灵亦引言很多用户在使用日志服务SLS时,采用STS这种免登的方式,能够快速将日志服务的页面集成到三方的系统中。STS模式是非常经典且成熟的免登方案,它的优点非常多,例如:能够充分利用阿里云账号RAM体系、能够通用地集成支持STS模式的所有云产品、支持客户自定义访问权限......
  • 【RedHat9.0】使用源代码安装软件——源代码安装nginx
    如果yum、rpm软件包不能提供所需的软件,就要考虑源码安装,获取源码包,进行编译安装。源码安装步骤:1.下载和解压软件包Linux、UNIX最新的软件通常以源代码打包的形式发布,最常见的是.tar.gz和.tar.bz2两种压缩包格式。下载完源代码包后需要解压缩。tar-zxvffile.tar.gzt......
  • nginx 正向代理
    环境规划序号IP地址操作系统用途1192.168.174.126(外网)192.168.80.128(内网)ubuntu23.10代理服务器2192.168.80.129(内网)ubuntu23.10Linux客户端软件版本nginx:1.24.0代理模块:proxy_connect_rewrite_102101.patch安装nginx#......
  • Nginx底层基础数据结构
    基础数据结构ngx_int_t32位操作系统4字节,64位操作系统8字节解决跨平台以及,普通int类型在x86和x64操作系统上面是4字节,在类型转换时造成内存浪费(如在x64下面转换long类型)typedefintptr_tngx_int_t;#ifdef_WIN64typedef__int64intptr_t;#elsetype......
  • 解释nginx.conf.default 中关于fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_sc
    在nginx配置中,fastcgi_param指令用于定义传递给FastCGI服务器(如PHP-FPM)的参数。这些参数是FastCGI协议的一部分,用于在nginx和PHP-FPM之间传递关于请求的信息。在给出的配置片段中:nginxfastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;这一行定义了一个FastCGI......
  • nginx 反向代理注意
    1.Nginx中正则捕获组在proxy_pass中不可用有一个需求,根据不同的路径代理到后端问题出在$1这个正则捕获组上。在Nginx中,正则表达式中的捕获组是根据()定义的,但这个捕获组只在location内部有效,并且不会传递到proxy_pass中。所以当你在proxy_pass中使用$1时,它实......
  • Nginx服务启动失败
    问题描述:Nginx服务启动失败解决方案:查看是否有如下服务起来,有需要停止服务并改为手动SQLServer导致。其中很有可能是SQLServerReportingServices(MSSQLSERVER),它是SQLServer的日志系统。IIS服务。如果你电脑安装了这个,很有可能它在运行着,那么它就占用着80端口Worl......
  • EAS_记录单据上机操作日志
    @Override protectedvoid_delete(Contextctx,IObjectPKpk)throwsBOSException,EASBizException{ CollectionRlptInfoinfo=this.getCollectionRlptInfo(ctx,pk);IObjectPKlogpk=LogUtil.beginLog(ctx,"_delete",newCollectionRlptInfo()......