搜索

首页  >  问答  >  正文

Nginx proxy_pass如何记录response?

使用了两种方式记录response日志,但是发现两种日志的数据都是乱码,怀疑是直接记录了内存中的数据,这是什么原因呢?

第一种方式:

采用yexiaobai大神的方式,源地址如下:
/blog/yexiaobai/1190000000606867

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<code>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/;

    }

    }

}

</code>

第二种方式:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<code>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/;

}

</code>

某草草某草草2798 天前677

全部回复(1)我来回复

  • 我想大声告诉你

    我想大声告诉你2017-05-16 17:27:07

    已解决,解决方案:

    1

    2

    3

    4

    5

    6

    7

    <code>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])';

    }

    </code>

    回复
    0
  • 取消回复