How can I remove backslash when log format use escape=json

Maxim Dounin mdounin at mdounin.ru
Thu Oct 18 13:41:02 UTC 2018


Hello!

On Thu, Oct 18, 2018 at 09:29:31AM -0400, kimown wrote:

> Hi, I want to log my entire request_body, but access.log contains some
> strange backslash, how can I remove these backslash before doube quote?
> 
> Here is my nginx.conf,
> ```
> log_format  main escape=json '$request_body';
> 
>  access_log  logs/access.log  main;
> ``` 
> 
> This is my request code:
> ```
> fetch('http://localhost:8080/njs',{
> method:'POST',
> body:JSON.stringify({
> text:'message with backslash'
> })
> }).then(res=>res.json()).then((res)=>{
> console.info(res)
> })
> ```
> 
> And access.log 
> ```
> {\"text\":\"message with backslash\"}
> ```
> 
> But I think it should be 
> ```
> {"text":"message with backslash"}
> ```

With "escape=json", nginx will escape variables to be usable in 
JSON structures, so you will be able to use them in log format 
with JSON formatting, e.g.:

log_format main escape=json '{ "body": "$request_body" }';

If you want nginx to do not escape anything in your logs, you can 
use "escape=none".  Note though that without escaping you 
completely depend on data the variable contain - in particular, in 
your case a malicious client will be able to supply arbitrary 
data, including multiple log entries or broken records.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list