Nginx proxy_pass如何記錄response?
某草草
某草草 2017-05-16 17:25:07
0
1
611

使用了兩種方式來記錄response日誌,但發現兩種日誌的數據都是亂碼,懷疑是直接記錄了記憶體中的數據,這是什麼原因呢?

第一種方式:

採用yexiaobai大神的方式,來源位址如下:
/blog/yexiaobai/1190000000606867

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $body_bytes_sent '
        '"$http_referer" "$http_user_agent" $request_time
        req_body:"$request_body" resp_body:"$resp_body"';
    server {
    listen       8088;
    server_name  localhost;
    access_log /tmp/nginx.resp.access.log log_req_resp;
    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
    ';
    location / {
        proxy_pass http://173.194.14.40/;
    }
    }
}

第二種方式:

location /test {
    content_by_lua '
        -- res = ngx.location.capture("http://173.194.14.57/")
        local res = ngx.location.capture("/", { share_all_vars = true })
        file = io.open("/tmp/capture.log", "a+")
        file:write(res.body)
        file:close()
        -- ngx.var.resp_body = res.body
        ngx.print(res_s)
';

}

location / {
    proxy_pass http://173.194.14.40/;
}
某草草
某草草

全部回覆(1)
我想大声告诉你

已解決,解決方案:

error_log /tmp/nginx.resp.info.log info;

location / {
    proxy_pass http://vpsea.flvcd.com/;
    body_filter_by_lua 'ngx.log(ngx.INFO, ngx.arg[1])';
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!