Nginx 使用 lua-nginx-module 来获取post请求中的request和response信息

news/2024/6/28 22:25:17

如果想要在nginx中打印出 http request 的所有 header,需要在编译nginx时开启
1、安装编译所需的依赖

apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

2、创建下载路径

mkdir -p /opt/download

3、下载所需的文件

# 不要下载官网http://luajit.org/download/的,要去下载openresty的优化版本 后面会说出现的坑
luajit: wget --no-check-certificate https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20210510.tar.gz
lua-nginx-module: wget --no-check-certificate https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.20.tar.gz
lua-resty-core: wget --no-check-certificate https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.22.tar.gz
lua-resty-lrucache: wget --no-check-certificate https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.11.tar.gz
tengine-2.3.2(nginx-1.17.3): wget --no-check-certificate http://tengine.taobao.org/download/tengine-2.3.2.tar.gz
ngx_devel_kit-0.3.1: wget --no-check-certificate https://github.com/vision5/ngx_devel_kit/archive/refs/tags/v0.3.1.tar.gz
openssl-1.1.1f: wget --no-check-certificate https://www.openssl.org/source/old/1.1.1/openssl-1.1.1f.tar.gz

解压所有文件

ls *.tar.gz | xargs -n1 tar xzvf

安装并添加luajit环境变量

cd luajit2-2.1-20210510
make install  PREFIX=/usr/local/LuaJIT
# 编辑环境便利文件
vim /etc/profile.d/lua.sh
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.1
# 引用环境变量
source /etc/profile.d/lua.sh 

安装lua_core

cd lua-resty-core-0.1.22
make install PREFIX=/usr/local/lua_core
cd ../lua-resty-lrucache-0.11
make install PREFIX=/usr/local/lua_core

编译安装 tengine(nginx)

cd tengine-2.3.2
./configure --prefix=/usr/local/nginx-1.17.3 --with-http_stub_status_module --with-http_gzip_static_module --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --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_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-stream --with-openssl=/opt/download/openssl-1.1.1f --add-module=/opt/download/lua-nginx-module-0.10.20 --add-module=/opt/download/ngx_devel_kit-0.3.1 --add-module=modules/ngx_http_upstream_check_module --add-module=modules/ngx_http_reqstat_module --add-module=modules/ngx_http_sysguard_module --add-module=modules/ngx_http_upstream_session_sticky_module --add-module=modules/ngx_http_user_agent_module --add-module=modules/ngx_slab_stat
make
make install

加载lua库,加入到ld.so.conf文件

cat  /etc/ld.so.conf.d/libc.conf
/usr/local/lib
/usr/local/LuaJIT/lib
# 加载
ldconfig

配置并测试lua环境

添加在http模块下
# 指定lua模块路径,多个之间";"分隔,其中";;"表示默认搜索路径,默认到nginx的根目录下找
lua_package_path "/usr/local/Lua_core/lib/lua/?.lua;;";
#指定server配置文件目录
include /usr/local/nginx-1.17.3/conf/conf.d/*.conf;
#自定义日志格式
log_format test escape=json '{'
    '"time": "$time_iso8601", '
    '"server_name": "$host", '
    '"port": "$server_port", '
    '"method": "$request_method", '
    '"args": "$args", '
    '"uri": "$uri", '
    '"server_protocol": "$server_protocol", '
    '"status": "$status", '
    '"body_bytes_sent": "$body_bytes_sent", '
    '"upstream_addr": "$upstream_addr", '
    '"upstream_status": "$upstream_status", '
    '"upstream_response_time": "$upstream_response_time", '
    '"req_header": "$req_header"'
    '}';

测试域名配置文件

server {
    listen 80;
    server_name www.test.com;
    access_log /var/log/nginx/www.test.com.access.log test;

	set $req_header "";
	header_filter_by_lua '
	  local h = ngx.req.get_headers()
	  for k, v in pairs(h) do
	      ngx.var.req_header = ngx.var.req_header .. k.."="..v.." "
	  end
	';


    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        # requset by yanshul
        proxy_intercept_errors on;
        error_page 301 = @success;

        proxy_pass http://172.17.64.32:31593;
    }


    location /lua {
        set $test "hello,world";
        content_by_lua '
            ngx.header.content_type="text/plain"
            ngx.say(ngx.var.test)';
    }
}

启动nginx

/usr/local/nginx-1.17.3/sbin/nginx

curl 测试请求

curl 127.0.0.1/lua -H 'host: www.test.com' -H 'test: hjy'

日志

{"time": "2023-08-24T08:03:53+00:00", "server_name": "www.test.com", "port": "80", "method": "GET", "args": "", "uri": "/lua", "server_protocol": "HTTP/1.1", "status": "200", "body_bytes_sent": "22", "upstream_addr": "", "upstream_status": "", "upstream_response_time": "", "req_header": "test=hjy user-agent=curl/7.58.0 accept=*/* host=www.test.com "}

看到日志中 req_header 字段输出所有request header


http://lihuaxi.xjx100.cn/news/1479037.html

相关文章

MySQL回表是什么?哪些情况下会回表

🏆作者简介,黑夜开发者,全栈领域新星创作者✌,CSDN博客专家,阿里云社区专家博主,2023年6月CSDN上海赛道top4。 🏆数年电商行业从业经验,历任核心研发工程师,项目技术负责…

开源与专有软件:比较与对比

🌷🍁 博主猫头虎 带您 Go to New World.✨🍁 🦄 博客首页——猫头虎的博客🎐 🐳《面试题大全专栏》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺 &a…

stm32 之20.HC-06蓝牙模块

原理图显示使用usart3串口使用的是PB10和PB11引脚 直接配置usart3串口协议 void usart3_init(uint32_t baud) {GPIO_InitTypeDef GPIO_InitStructureure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;//端口B硬件时钟打开RCC_AHB1PeriphClockC…

使用devsidecar 软件解决 git因网络问题报错的

上链接: https://github.com/docmirror/dev-sidecar/releases 下载你系统对应的版本 安装后根据教程设置即可 解决了git提交、拉取时报以下错误: Failed to connect to github.com port 443 after 21051 ms: Couldnt connect to server Recv failur…

基于数据湖的多流拼接方案-HUDI概念篇

目录 一、为什么需要HUDI? 1. 传统技术选型存在哪些问题? 2. Hudi有什么优点? 基于 Hudi Payload 机制的多流拼接方案: 二、HUDI的应用场景 1. 什么场景适合使用hudi? 2. 什么场景不适合使用hudi? …

C#使用xamarin进行跨平台开发

使用 Xamarin 进行跨平台开发可以使用 C# 和 .NET 平台来开发移动应用程序,同时将代码在多个主要移动操作系统上运行,包括 Android 和 iOS。以下是在 C# 中使用 Xamarin 进行跨平台开发的一般步骤: 安装 Xamarin: 在开始之前&…

ASE入门系列

cast shadows、receive shadows 和阴影相关(投射阴影和自身阴影),关闭,阴影消失; queue index 队列索引,不透明和半透明物体有一个默认队列,不透明物体,默认队列是2000&#xff0c…

前端面试:【性能优化】前端缓存、CDN、懒加载和预加载

亲爱的前端开发者,Web性能对用户体验至关重要。如果你想让你的网站更快、更具吸引力,就需要关注前端性能优化。在这篇文章中,我们将深入探讨四个关键的性能优化策略:前端缓存、CDN(内容分发网络)、懒加载和…