The default empty field for nginx access log is-

in the first case, the empty field is "-", which is what I want, as long as it is not "", but the double quotation marks of resp_body become x22

.
-sharp log_format
log_format  main  ""$remote_addr" "$remote_user" "$time_local" "$request" "
                      ""$status" "$body_bytes_sent" "$http_referer" "
                      ""$http_user_agent" "$http_x_forwarded_for" "$request_time" "
                      ""$request_body" "$resp_body"";
                      
-sharp 
"127.0.0.1" "-" "16/Jan/2019:11:24:05 +0800" "POST /test HTTP/1.1" "200" "20" "-" "curl/7.47.0" "-" "0.001" "-" "{\x22data\x22: \x22asasddsf\x22}"
In the second case, resp_body is fine, but the empty field is "". I want to have a default value .
-sharp log_format
log_format  main escape=none ""$remote_addr" "$remote_user" "$time_local" "$request" "
                      ""$status" "$body_bytes_sent" "$http_referer" "
                      ""$http_user_agent" "$http_x_forwarded_for" "$request_time" "
                      ""$request_body" "$resp_body"";
-sharp
"127.0.0.1" "" "16/Jan/2019:11:47:07 +0800" "POST /test HTTP/1.1" "200" "20" "" "curl/7.47.0" "" "0.002" "" "{"data": "asasddsf"}"

can you output the quotation marks normally in the first case, or the default value of the empty field in the second case, if the default value is -, output "-"

Apr.28,2022

modified the source code src/http/modules/ngx_http_log_module.c and recompiled it when the space-time value of escape=none returned. But it feels like there should be a better way.

ngx_http_log_unescaped_variable(ngx_http_request_t *r, u_char *buf,
    ngx_http_log_op_t *op)
{
    ngx_http_variable_value_t  *value;

    value = ngx_http_get_indexed_variable(r, op->data);

    if (value == NULL || value->not_found) {
        -sharp -
        *buf = '-';
        return buf + 1;
    }

    return ngx_cpymem(buf, value->data, value->len);
}
Menu