Skip to content

Instantly share code, notes, and snippets.

@kaichao
Last active April 29, 2025 03:26
Show Gist options
  • Save kaichao/077340aa66e8cdf858577a93a9fe1b0d to your computer and use it in GitHub Desktop.
Save kaichao/077340aa66e8cdf858577a93a9fe1b0d to your computer and use it in GitHub Desktop.

Revisions

  1. kaichao revised this gist Apr 28, 2020. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions log-http-headers.md
    Original file line number Diff line number Diff line change
    @@ -37,5 +37,8 @@
    'req_body:"$request_body" resp_body:"$resp_body"';
    ```

    request headers $http_<header>
    sent headers $sent_http_<header>
    ## individual headers
    ```
    request headers:$http_<header>
    sent headers: $sent_http_<header>
    ```
  2. kaichao revised this gist Apr 28, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions log-http-headers.md
    Original file line number Diff line number Diff line change
    @@ -36,3 +36,6 @@
    '$request_time req_header:"$req_header" resp_header:"$resp_header" '
    'req_body:"$request_body" resp_body:"$resp_body"';
    ```

    request headers $http_<header>
    sent headers $sent_http_<header>
  3. kaichao revised this gist Apr 28, 2020. No changes.
  4. kaichao revised this gist Apr 28, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions log-http-headers.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ## switch nginx image to openresty/openresty
    ## 1. switch nginx image to openresty/openresty

    ## add the following to server/location (/etc/nginx/conf.d/default.conf)
    ## 2. add the following to server/location (/etc/nginx/conf.d/default.conf)
    ```
    set $req_header "";
    set $resp_header "";
    @@ -29,7 +29,7 @@
    access_log /var/log/nginx-access.log log_req_resp;
    ```

    ## add the following to http (/usr/local/openresty/nginx/conf/nginx.conf)
    ## 3. add the following to http (/usr/local/openresty/nginx/conf/nginx.conf)
    ```
    log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" '
  5. kaichao created this gist Apr 28, 2020.
    38 changes: 38 additions & 0 deletions log-http-headers.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    ## switch nginx image to openresty/openresty

    ## add the following to server/location (/etc/nginx/conf.d/default.conf)
    ```
    set $req_header "";
    set $resp_header "";
    header_filter_by_lua_block{
    local h = ngx.req.get_headers();
    for k, v in pairs(h) do
    ngx.var.req_header = ngx.var.req_header .. k.."="..v.." ";
    end
    local rh = ngx.resp.get_headers();
    for k, v in pairs(rh) do
    ngx.var.resp_header = ngx.var.resp_header .. k.."="..v.." ";
    end
    }
    lua_need_request_body on;
    set $resp_body "";
    body_filter_by_lua_block {
    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 /dev/stdout log_req_resp;
    access_log /var/log/nginx-access.log log_req_resp;
    ```

    ## add the following to http (/usr/local/openresty/nginx/conf/nginx.conf)
    ```
    log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" '
    '$request_time req_header:"$req_header" resp_header:"$resp_header" '
    'req_body:"$request_body" resp_body:"$resp_body"';
    ```