Nginx平滑处理echo模块收集POST日志

2023年 7月 15日 30.4k 0

Nginx可以轻松处理大量的HTTP流量。每次NGINX处理连接时,将生成一个日志条目,以存储此连接(例如远程IP地址,响应大小和状态代码等)的某些信息。可在此处找到包含更多详细信息的完整记录信息集。

在某些情况下,您可能更愿意存储请求的主体,特别是POST请求。幸运的是,NGINX生态系统是丰富的,并且包括很多 方便的模块。一个这样的模块是 回声模块,它提供的东西等是有用的功能:echo,time,和sleep 命令。

在我们的用例中,要记录请求体,我们需要的是使用echo_read_request_body命令和$request_body变量(包含Echo模块的请求体)。然而,这个模块不是默认分配给NGINX,为了能够使用它,我们必须通过构建包含Echo模块的源代码的源代码构建NGINX。

以下步骤详细介绍了如何构建NGINX以便包含Echo模块(这里是完整的构建bash文件)

使用以下命令下载NGINX和Echo的源代码:

[root@linuxea ]# curl -Lk https://github.com/openresty/echo-nginx-module/archive/v0.58.tar.gz -o /usr/local/ 
[root@linuxea ]# tar xf v0.58.tar.gz -C /tmp/echo-nginx-module

源nginx安装目录位于/usr/local/webserver/nginx,我们直接下载同样版本的nginx,进行编译

./configure --user=www 
--group=www 
--prefix=/usr/local/webserver/nginx 
--with-http_stub_status_module 
--with-http_ssl_module 
--with-http_gunzip_module 
--with-http_mp4_module 
--with-http_flv_module 
--with-pcre 
--with-http_gzip_static_module 
--with-http_realip_module 
--with-ld-opt=-ljemalloc 
--add-module=/tmp/echo-nginx-module

编译完成后,仅仅只进行make即可在/usr/local/webserver/nginx/sbin/下,删除或者重新命名nginxmv nginx old_nginxmv或者rm后nginx还是在运行着

[root@linuxea sbin]# ps aux|grep nginx
root      1796  0.0  0.9 124408 36180 ?        Ss   Mar06   0:02 nginx: master process /usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
www      15851  0.3  2.0 169464 78680 ?        S    05:05   0:00 nginx: worker process                                                              
www      15852  0.6  1.9 169464 78072 ?        S    05:05   0:00 nginx: worker process                                                              
www      15853  0.5  1.9 169464 77168 ?        S    05:05   0:00 nginx: worker process                                                              
www      15854  0.3  2.0 169464 78680 ?        S    05:05   0:00 nginx: worker process                                                              
www      15855  0.3  2.0 169464 78600 ?        S    05:05   0:00 nginx: worker process                                                              
www      15856  1.7  1.9 171512 77972 ?        S    05:05   0:00 nginx: worker process                                                              
[root@linuxea sbin]# cp /usr/local/nginx-1.8.0/objs/nginx /usr/local/webserver/nginx/sbin/
[root@linuxea sbin]# /etc/init.d/nginx reload

nginx主配置文件添加:

log_format upstream2 '$proxy_add_x_forwarded_for $remote_user [$time_local] "$request" $http_host'
        '$body_bytes_sent $request_body "$http_referer" "$http_user_agent" $ssl_protocol $ssl_cipher'
        '$request_time [$status] [$upstream_status] [$upstream_response_time] "$upstream_addr"';
在server段添加:
        echo_read_request_body;
        access_log  /data/logs/wwwlogs/access.log upstream2;

查看日志:20170314172116.png 脚本;

if ! rpm -ql GeoIP-devel >/dev/null 2>&1;then yum install GeoIP-devel -y;fi
mkdir -p /tmp/nginx_build/{echo-nginx-module,ngx_http_geoip2_module}
curl -Lks http://nginx.org/download/nginx-1.10.3.tar.gz|tar -xz -C /tmp/nginx_build/  --strip-components=1
curl -Lks $(curl -Lks 'https://github.com/openresty/echo-nginx-module/releases'| awk -F'"' '/tar.gz"/{print "https://github.com"$2;exit}')| tar -xz -C /tmp/nginx_build/echo-nginx-module/ --strip-components=1
cd /tmp/nginx_build/ngx_http_geoip2_module
git clone https://github.com/voxxit/dockerfiles.git
mv dockerfiles/nginx-geoip2/ngx_http_geoip2_module-1.0/* . && cd ../
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gunzip_module --with-http_mp4_module --with-http_flv_module --with-http_realip_module --with-pcre --with-http_gzip_static_module --with-ld-opt=-ljemalloc --add-module=./echo-nginx-module --with-http_geoip_module
make -j$(getconf _NPROCESSORS_ONLN) && make install && cd && rm -rf /tmp/nginx_build
echo 'export PATH=/usr/local/webserver/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
. /etc/profile.d/nginx.sh && nginx -V

本文参考:https://developers.redhat.com/blog/2016/05/23/configuring-nginx-to-log-post-data-on-linux-rhel/

相关文章

LeaferJS 1.0 重磅发布:强悍的前端 Canvas 渲染引擎
10分钟搞定支持通配符的永久有效免费HTTPS证书
300 多个 Microsoft Excel 快捷方式
一步步配置基于kubeadmin的kubevip高可用
istio全链路传递cookie和header灰度
REST Web 服务版本控制

发布评论