nginx安装增加lua模块配置http的response报文

9 篇文章 0 订阅
1 篇文章 0 订阅

主要参考两篇文章

mac安装nginx+lua

nginx日志中添加请求的response日志(推荐)

Nginx 使用 lua-nginx-module

主要问题还是依赖软件和依赖库导致。

基本

系统:腾讯云centos7
源码目录:/usr/local/src/
默认安装目录:/usr/local/
日志目录:/usr/local/nginx/log/access.log
加载指定目录配置:   include /etc/bbw_nginx/*.conf;

安装依赖

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre  pcre-devel

安装

 

cd /usr/local/src
wget https://codeload.github.com/openresty/lua-nginx-module/tar.gz/v0.10.7
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
wget https://nginx.org/download/nginx-1.11.5.tar.gz

tar zxf lua-nginx-module-0.10.7.tar.gz 
tar zxf LuaJIT-2.0.4.tar.gz 
tar zxf nginx-1.11.5.tar.gz 

cd LuaJIT-2.0.4/
make && make install

cat >> /etc/profile <<EOF
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
EOF
source /etc/profile

cd ../nginx-1.11.5/
# 配置需要的模块
./configure   --prefix=/usr/local/nginx --add-module=../lua-nginx-module-0.10.7

make -j2
make install  

测试安装:

配置1

  log_format  mylog  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      'request: "$request_body"' 'response: $resp_body';

配置2

      #记录nginx请求返回值
       lua_need_request_body on;
       set $resp_body "";
       body_filter_by_lua '
         local resp_body = string.sub(ngx.arg[1], 1, 1000)
            ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
               if ngx.arg[2] then
                   ngx.var.resp_body = ngx.ctx.buffered
                      end
                        ';
        access_log  logs/host.access.log  mylog;

配置3

   location /helloTest {
            default_type 'text/plain';
            content_by_lua 'ngx.say("hello, lua")';
        }

验证:curl localhost/helloTest?name=weijia

[root@VM_226_29_centos sbin]# cat /usr/local/nginx-1.4.2/logs/host.access.log
response_body:hello, lua\x0A
127.0.0.1 | - | hello, lua\x0A
127.0.0.1 - - [02/Nov/2019:16:00:36 +0800] "GET /helloTest?name=weijia HTTP/1.1" 200 21 "-" "curl/7.29.0" "-"request: "-"response: hello, lua\x0A

整体文件配置位置


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
#配置1
    log_format  mylog  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      'request: "$request_body"' 'response: $resp_body';
    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
      #配置2
       lua_need_request_body on;
       set $resp_body "";
       body_filter_by_lua '
         local resp_body = string.sub(ngx.arg[1], 1, 1000)
            ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
               if ngx.arg[2] then
                   ngx.var.resp_body = ngx.ctx.buffered
                      end
                        ';
        access_log  logs/host.access.log  mylog;

        location / {
            root   html;
            index  index.html index.htm;
        }
#配置3
        location /helloTest {
            default_type 'text/plain';
            content_by_lua 'ngx.say("hello, lua")';
        }

查看nginx配置:

[root@VM_226_29_centos sbin]# ./nginx -V
nginx version: nginx/1.4.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx-1.4.2 --add-module=../lua-nginx-module-0.8.6

问题

1、启动时会nginx可能会报错

./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: N

找不到libluajit-5.1.so.2这个文件
解决办法
1.找到 libluajit-5.1.so.2,libluajit-5.1.so.2.0.2这两个文件复制到 对应的lib下
64位是 /usr/lib64
32位是 /usr/lib

问题2

access.log日志中文乱码

nginx升级版本到1.12.1;lua模块升级v0.10.9rc7.tar.gz;增加参数escape=json

    log_format  main escape=json  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      'request: "$request_body"' 'response: $resp_body';

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值