• Nginx日志配置及状态监控


    一 Nginx请求简介

    1.1 请求头部

    对于HTTP而言,客户端负责发起request请求,服务端负责response响应。
    request:包括请求行、请求头部、请求数据;
    response:包括状态行、消息报头、响应正文。
    [root@master conf.d]# curl -v 192.168.1.220
    * About to connect() to 192.168.1.220 port 80 (#0)  ##关于本次连接信息
    *   Trying 192.168.1.220...
    * Connected to 192.168.1.220 (192.168.1.220) port 80 (#0)
    > GET / HTTP/1.1   #HTTP版本
    > User-Agent: curl/7.29.0    #客户端信息
    > Host: 192.168.1.220   #请求的服务端主机
    > Accept: */*   #如上为请求
    > 
    < HTTP/1.1 200 OK   #返回http版本
    < Server: nginx/1.18.0    #服务端Web类型
    < Date: Sun, 22 Nov 2020 09:40:16 GMT   #日期时间
    < Content-Type: text/html   #返回的类型
    < Content-Length: 612    #长度
    < Last-Modified: Thu, 29 Oct 2020 15:25:17 GMT   #日期时间
    < Connection: keep-alive   #长连接
    < ETag: "5f9adedd-264"   #Etag
    < Accept-Ranges: bytes     #大小单位
    < 
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>    #具体内容
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    * Connection #0 to host 192.168.1.220 left intact
    [root@master conf.d]# 

    二 日志配置

    2.1 日志相关配置

    nginx日志相关涉及的配置有:
    access_log:访问日志;
    log_format:日志格式;
    rewrite_log:重定向日志;
    error_log:错误日志;
    open_log_file_cache、log_not_found、log_subrequest。
    nginx具备非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。
    日志格式通过log_format命令来定义。ngx_http_log_module:用于定义请求日志格式。

    2.2 access_log配置

    语法:
    • access_log path [format [buffer=size [flush=time]]];
    • access_log path format gzip[=level] [buffer=size] [flush=time];
    • access_log syslog:server=address[,parameter=value] [format];
    • access_log off; #不记录日志
    默认值: access_log logs/access.log combined;
    使用默认combined格式记录日志:access_log logs/access.log 或 access_log logs/access.log combined;
    配置段: http, server, location, if in location, limit_except
    参数解释:
    • gzip:压缩等级。
    • buffer:设置内存缓存区大小。
    • flush:保存在缓存区中的最长时间。

    2.3 log_format配置

    语法:
    • log_format name string ……;
    默认值: log_format combined "……";
    配置段: http
    释义:name表示格式名称,string表示等义的格式。log_format有一个默认的无需设置的combined日志格式,相当于apache的combined日志格式。
    示例1:
      1 ……
      2     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
      3                       '$status $body_bytes_sent "$http_referer" '
      4                       '"$http_user_agent"';
      5 ……
     
    示例2:
      1 ……
      2     log_format  proxy  '$remote_addr - $remote_user [$time_local] "$request" '
      3                       '$status $body_bytes_sent "$http_referer" '
      4                       '"$http_user_agent" "$http_user_agent" ';
      5 ……
     
    配置相关变量释义:
    $remote_addr:表示客户端地址;
    $remote_user:表示http客户端请求Nginx认证的用户名;
    $time_local:Nginx通用日志格式下的本地时间;
    $request:request请求行,请求的URL、GET等方法、HTTP协议版本;
    $request_length:请求的长度;
    $request_time:请求处理时间,单位为秒,精度为毫秒;
    $status:response返回状态码;
    $body_bytes_sent:发送给客户端的字节数,不包括响应头的大小,即服务端响应给客户端body信息大小;
    $http_referer:http上一级页面,即从哪个页面链接访问过来的,用于防盗链、用户行为分析;
    $http_user_agent:http头部信息,记录客户端浏览器相关信息;
    $connection:连接的序列号;
    $connection_requesta:当前通常一个连接获得的请求数量;
    $msec:日志写入时间,单位为秒,精度为毫秒;
    $pipe:如果请求是通过HTTP流水线(pipelined)发送,pipe值为‘p’,否则为“.”;
    $http_x_forwarded_for:http请求携带的http信息。
    提示:如果nginx位于负载均衡器,squid,nginx反向代理之后,web服务器无法直接获取到客户端真实的IP地址了。 $remote_addr获取反向代理的IP地址。反向代理服务器在转发请求的http头信息中,可以增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。

    2.4 open_log_file_cache配置

    语法:
    open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
    open_log_file_cache off;
    默认值:open_log_file_cache off; #关闭open_log_file_cache
    配置段:http,server,location
    作用:对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off)。
    参数释义:
    max:设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
    inactive:设置存活时间,默认是10s。
    min_uses:设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次。
    valid:设置检查频率,默认60s。
    off:禁用缓存。
    示例1:
    open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

    2.5 log_not_found配置

    语法:log_not_found on | off;
    默认值:log_not_found on;
    配置段:http,server,location
    作用:是否在error_log中记录不存在的错误,默认是,即记录。

    2.6 log_subrequest配置

    语法:log_subrequest on | off;
    默认值:log_subrequest off;
    配置段:http,server,location
    作用:是否在access_log中记录子请求的访问日志,默认否,即不记录。

    2.7 rewrite_log配置

    语法: rewrite_log on | off;
    默认值:rewrite_log off;
    配置段:http,server,location,if
    作用:由ngx_http_rewrite_module模块提供的。用来记录重写日志的,对于调试重写规则建议开启。启用时将在error log中记录notice级别的重写日志。

    2.8 error_log配置

    语法:error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];
    默认值:error_log logs/error.log error;
    配置段:main,http,server,location
    作用:配置错误日志。

    三 状态监控

    3.1 配置监控

    Nginx状态监控使用--with-http_stub_status_modele编译模块,语法:
    语法:stub_status on | off;
    默认值:stub_status off;
    配置段:server,location
    示例01:
    [root@master conf.d]# cat status.conf 
    server {
        server_name  192.168.1.220;
    
        error_page  404 403 500 502 503 504  /error.html;
        location = /error.html {
            root    /usr/share/nginx/html;
        }
    
        location / {
            root    /usr/share/nginx/blog;
            index   index.html;
        }
        location /ok {
            alias   /usr/share/nginx/yes;
            index   index.html;
        }
        location /mystatus {
            stub_status on;
            access_log off;
        }
    }
    [root@master conf.d]# nginx -t -c /etc/nginx/nginx.conf
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@master conf.d]# nginx -s reload
    [root@master conf.d]# curl http://192.168.1.220/mystatus
    Active connections: 1 
    server accepts handled requests
     9 9 7 
    Reading: 0 Writing: 1 Waiting: 0 
    [root@master conf.d]# curl http://192.168.1.220/mystatus
    Active connections: 1 
    server accepts handled requests
     10 10 8 
    Reading: 0 Writing: 1 Waiting: 0 
    [root@master conf.d]# curl http://192.168.1.220/mystatus
    Active connections: 1 
    server accepts handled requests
     11 11 9 
    Reading: 0 Writing: 1 Waiting: 0 
    [root@master conf.d]# curl http://192.168.1.220/mystatus
    Active connections: 1 
    server accepts handled requests
     12 12 10 
    Reading: 0 Writing: 1 Waiting: 0 
    [root@master conf.d]# 
    释义:
    • Active connections:当前活跃的连接数。
    • server:表示Nginx启动到现在共处理了90个连接。
    • accepts:表示Nginx启动到现在共成功创建90次握手。
    • handled requests:表示总共处理了19次请求。
    • Reading:Nginx读取到客户端的 Header 信息数。
    • Writing:Nginx返回给客户端的 Header 信息数。
    • Waiting:Nginx开启keep-alive长连接情况下, 既没有读也没有写, 建立连接情况。
    请求丢失数=(握手数-连接数)可以看出,本次状态显示没有丢失请求。
    [root@master conf.d]# mkdir -p  /usr/share/nginx/{blog,yes}
    [root@master conf.d]# echo "blog" >  /usr/share/nginx/blog/index.html
    [root@master conf.d]# echo "yes" >  /usr/share/nginx/yes/index.html
    [root@master conf.d]# curl http://192.168.1.220
    blog
    [root@master conf.d]# curl http://192.168.1.220/ok/
    yes
  • 相关阅读:
    黑域,黑阈 Permission denied
    手机闪存速度测试工具,AndroBench
    找进程的窗口Handle
    nginx http 正向代理
    windows命令行netstat 统计连接数
    FLIR ONE PRO热成像仪
    python2.0_s12_day14_jQuery详解
    python2.0_s12_day13_javascript&Dom&jQuery
    ssh&scp指定密钥
    apache+php生产环境错误记录
  • 原文地址:https://www.cnblogs.com/liujunjun/p/14020178.html
Copyright © 2020-2023  润新知