• nginx 配置日志格式并且采集POST请求信息放入日志


    1、nginx采集post请求日志有两种方式:

    1.可以安装 openresty 版本nginx;

    2.可以使用官方nginx版本编译lua模块;

    在这里我们就使用nginx版本编译lua模块进行配置, openresty 版本nginx就不做更多说明:

    查看默认yum安装的nginx的相关版本及其模块: 主要是编译的时候没有编译 ngx_http_lua_module.so  模块,可以通过 nginx -V命令查看当前Nginx的以编译模块:

    # nginx -V
    nginx version: nginx/1.16.1
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
    built with OpenSSL 1.0.2k-fips 26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

    查看编译模块目录:

    # cd /usr/lib64/nginx/modules
    # ls
    ngx_http_image_filter_module.so ngx_http_perl_module.so ngx_http_xslt_filter_module.so ngx_mail_module.so ngx_stream_module.so

    注意:

    需要让nginx有POST信息的日志,只需要把 ngx_http_lua_module.so 加载进去即可,以下是一个简单的办法yum的方式安装带有 ngx_http_lua_module.so 模块的nginx

    使用appnode的yum安装nginx, ngx_http_lua_module.so 就可以手动的加载

    添加yum源:

    [appnode]
    name=AppNode's RPM repository for Enterprise Linux 7 - x86_64
    baseurl=http://repo.appnode.com/stable/el/7/x86_64/
    enabled=1
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-appnode
     
    [appnode-development]
    name=AppNode's RPM repository for Enterprise Linux 7 - x86_64
    baseurl=http://repo.appnode.com/development/el/7/x86_64/
    enabled=0
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-appnode
     
    [appnode-archive]
    name=AppNode's RPM repository for Enterprise Linux 7 - x86_64
    baseurl=http://repo.appnode.com/archive/el/7/x86_64/
    enabled=0
    gpgcheck=0

    安装appnode中的nginx

    yum install appnode-nginx-stable  appnode-nginx-stable-module-ndk  appnode-nginx-stable-module-geoip appnode-nginx-stable-module-brotli  appnode-nginx-stable-module-lua

    查看安装的包:

    # rpm -qa |grep nginx
    appnode-nginx-stable-module-brotli-1.16.1.1.0.4.20180417.37ab9b2-1.el7.x86_64
    appnode-nginx-stable-1.16.1-1.el7.x86_64
    appnode-nginx-stable-module-lua-1.16.1.0.10.13-1.el7.x86_64
    appnode-nginx-stable-module-ndk-1.16.1.0.3.0-1.el7.x86_64
    appnode-nginx-stable-module-geoip-1.16.1-1.el7.x86_64

    再次查看安装的nginx编译的模块:

    # cd /usr/lib64/nginx/modules
    # ls
    ndk_http_module-debug.so ngx_http_brotli_filter_module-debug.so ngx_http_brotli_static_module-debug.so ngx_http_geoip_module-debug.so ngx_http_lua_module-debug.so ngx_stream_geoip_module-debug.so
    ndk_http_module.so ngx_http_brotli_filter_module.so ngx_http_brotli_static_module.so ngx_http_geoip_module.so ngx_http_lua_module.so ngx_stream_geoip_module.so

    nginx配置文件:

    # cat /etc/nginx/nginx.conf
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
     
    # 加载 ngx_http_lua_module 模块,前提是 ngx_http_lua_module 存在
    load_module modules/ngx_http_lua_module.so;
    include /usr/share/nginx/modules/*.conf;
     
    events {
     worker_connections 1024;
    }
     
    http {
     log_format main '$remote_addr - $remote_user [$time_local] "$request" '
     '$status $body_bytes_sent "$http_referer" '
     '"$http_user_agent" "$http_x_forwarded_for" '
     '$request_time $upstream_response_time';
    # nginx日志输出为 json格式
    # $request_body 是加载 ngx_http_lua_module 模块后特有的变量
    log_format json escape=json '{ "@timestamp": "$time_iso8601", '
    '"remote_addr": "$remote_addr", '
    '"remote_user": "$remote_user", '
    '"request": "$request", '
    '"request_body": "$request_body", '
    '"status": $status, '
    '"bytes": $body_bytes_sent, '
    '"referer": "$http_referer", '
    '"agent": "$http_user_agent", '
    '"x_forwarded": "$http_x_forwarded_for", '
    '"upstr_addr": "$upstream_addr",'
    '"upstr_host": "$upstream_http_host",'
    '"request_time": "$request_time", '
    '"ups_resp_time": "$upstream_response_time" }';
     
    access_log /var/log/nginx/access.log main;
     
    sendfile on;
     tcp_nopush on;
     tcp_nodelay on;
     keepalive_timeout 65;
     types_hash_max_size 2048;
     
    #nginx 安全相关配置
     #关闭文件索引
     autoindex on;
     #隐藏版本号
     server_tokens off;
     #设置客户端请求头读取超时时间,超过这个时间还没有发送任何数据,Nginx将返回"Request time out(408)"错误
     client_header_timeout 150;
     #设置客户端请求主体读取超时时间,超过这个时间还没有发送任何数据,Nginx将返回"Request time out(408)"错误
     client_body_timeout 150;
     #上传文件大小限制
     client_max_body_size 100m;
     #指定响应客户端的超时时间。这个超过仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。
     send_timeout 600;
     
    include /etc/nginx/mime.types;
     default_type application/octet-stream;
     
    # Load modular configuration files from the /etc/nginx/conf.d directory.
     # See http://nginx.org/en/docs/ngx_core_module.html#include
     # for more information.
     include /etc/nginx/conf.d/*.conf;
    }

    此时查看Nginx的日志内容时候就有相关post的请求信息了

  • 相关阅读:
    硬件_WIFI&Blue
    C++_练习—多态_纯虚函数与抽象类
    C++_练习—多态_验证vptr分布初始化
    C++_练习—多态_证明vptr指针的存在
    单摆方程
    谐振动相关知识
    UVa 129
    LeetCode-316
    Java 运算符
    一些特殊的矩阵
  • 原文地址:https://www.cnblogs.com/abner123/p/14137723.html
Copyright © 2020-2023  润新知