nginx安裝增加lua模塊配置http的response報文

主要參考兩篇文章

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';

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章