• Nginx日志格式


    默认Nginx日志格式

    http {
    ...
    
        #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  log/access.log  main;
    
    ...
    }
    
    默认格式main记录样例如下:
    192.168.1.1 - - [19/Mar/2022:13:28:27 +0800] "GET / HTTP/1.1" 200 14501 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"

    Nginx日志常用参数详解

    $remote_addr:记录访问网站的客户端地址
    $remote_user:远程客户端用户名称
    $time_local:记录访问时间与时区
    $request:表示request请求头的行
    $status:http状态码,记录请求返回的状态,例如:200、404、301等
    $body_bytes_sent:服务器发送给客户端的响应body字节数
    $http_referer:记录此次请求是从哪个链接访问过来的,可以根据refer进行防盗链设置
    $http_user_agent:记录客户端访问信息,例如:浏览器,手机客户端等
    $http_x_forwarded_for:当前端有代理服务器时,设置Web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的http_x_forwarded_for设置
    $ssl_protocol:SSL协议版本
    $ssl_cipher:交换数据中的算法
    $upstream_status:upstream状态
    $upstream_addr:当ngnix做负载均衡时,可以查看后台提供真实服务的设备
    $upstream_response_time:请求过程中,upstream响应时间
    $request_time:整个请求的总时间
    $args:这个变量等于请求行中的参数,同$query_string
    $content_length:请求头中的Content-length字段。
    $content_type:请求头中的Content-Type字段。
    $document_root:当前请求在root指令中指定的值。
    $host:请求主机头字段,否则为服务器名称。
    $http_user_agent:客户端agent信息
    $http_cookie:客户端cookie信息
    $limit_rate:这个变量可以限制连接速率。
    $request_method:客户端请求的动作,通常为GET或POST。
    $remote_addr:客户端的IP地址。
    $remote_port:客户端的端口。
    $remote_user:已经经过Auth Basic Module验证的用户名。
    $request_filename:当前请求的文件路径,由root或alias指令与URI请求生成。
    $scheme:HTTP方法(如http,https)。
    $server_protocol:请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
    $server_addr:服务器地址,在完成一次系统调用后可以确定这个值。
    $server_name:服务器名称。
    $server_port:请求到达服务器的端口号。
    $request_uri:包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
    $uri:不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
    $document_uri:与$uri相同。

    如何自定义日志格式

    [root@service conf]# vim /usr/local/nginx/conf/nginx.conf
    http {
    ...
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
        # 全局定义
        log_format  self_access  '$remote_addr - $remote_user [$time_local] "$request" '
                                 '$status $upstream_addr $upstream_status $upstream_response_time'
                                 '$request_time $body_bytes_sent "$http_referer" '
                                 '"$http_user_agent"';
        # 定义全局access日志并引用格式
        access_log  /var/log/nginx/access.log  self_access;
    ...
        server {
            listen 80;
            server_name www.test.com;
        # 定义当前server-access日志并引用格式
        access_log  /var/log/nginx/test.access.log  self_access; 
            location / {
        ...
            }
        }
    
    # include vhost/*.conf;
    }
    
    输出格式如下:
    192.168.1.1 - - [19/Mar/2022:16:03:53 +0800] "GET / HTTP/1.1" 304 192.168.1.10:80 304 0.0020.002 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
    192.168.1.1 - - [19/Mar/2022:16:03:53 +0800] "GET / HTTP/1.1" 200 192.168.1.20:80 200 0.0030.003 14501 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
  • 相关阅读:
    jquery及相关兼容插件的版本搭配
    jquery IE7 下报错:SCRIPT257: 由于出现错误 80020101 而导致此项操作无法完成
    兼顾php和jquery新版本的checkbox的写法
    jquery.validate动态更改校验规则 【转】
    Google的10大座右铭
    用jQuery屏蔽掉按回车键时提交表单
    sizeof()和strlen()在求字符串长度时的差别
    Matlab使用心得
    C++ 数据类型及相关问题 及输出精度控制
    原生javascript满屏上下滚动
  • 原文地址:https://www.cnblogs.com/funcquery/p/16026830.html
Copyright © 2020-2023  润新知