可以用openresty代替nginx+lua

Linux | 2020-07-06 13:14:16 | 阅读 1227 次 | 评论(0)

因为nginx安装lua扩展比较麻烦,所以直接安装openresty比较方便,安装使用方式跟nginx一样,默认集成了lua


wget https://openresty.org/download/openresty-1.17.8.1.tar.gz
tar zxvf openresty-1.17.8.1.tar.gz
cd openresty-1.17.8.1/
./configure --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module --with-http_realip_module;
make
make install


-------------------------------- 作者在 2020-07-06 16:23:08 补充以下内容 --------------------------------

在nginx.conf的http中加入:

    log_format mylog escape=json '\n\n---------------------------------------------------------------------------\n\n'
        '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"'
        '\n\n'
        'request: $request_body'
        '\n\n'
        'response: $resp_body';
在server中加入:
    lua_need_request_body on;
    set $resp_body "";
    body_filter_by_lua '
        local resp_body = string.sub(ngx.arg[1], 1, 10000)
        ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
        newStr = ngx.ctx.buffered
        newStr, n, err = ngx.re.gsub(newStr, [[\\\\/]], [[/]])
        newStr, n, err = ngx.re.gsub(newStr, [[\\\"]], [[\']])
        if ngx.arg[2] then
           ngx.var.resp_body = newStr
        end
    ';

    access_log  /var/www/www.xxx.com/log/access.log mylog;


文章评论,共0条
游客请输入验证码
浏览2132083次
文章归档