Welcome! Log In Create A New Profile

Advanced

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

Maxim Dounin
October 18, 2018 09:42AM
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/
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

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

Maxim Dounin October 18, 2018 09:42AM

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

kimown October 18, 2018 10:23PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 288
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready