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的請求信息了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM