How to set Nginx log printing post request parameters

How to set Nginx log printing post request parameters

【Foreword】

The SMS function of our project is to connect to a third party. The original receipt confirmation request from the third party was a get request. When we were troubleshooting, we could get the parameters of the request from the other party through the nginx log. Recently, we changed to another third party. The confirmation request from the new third party was a post. When we were troubleshooting, we found that nginx did not print the specific parameters. So we consulted some information and did experiments and online adjustments with the operation and maintenance. After the adjustment, we can get the request parameters, which is more convenient for us to troubleshoot the problem.

[Nginx sets print post request parameters]

1. Set the Nginx configuration file (nginx.conf) to print post request parameters: Add "dm":$request_body to the log_format of the http module to prevent Chinese garbled characters and add escape=json

 log_format main escape=json '{ "@timestamp": "$time_iso8601", '
            '"remote_addr": "$remote_addr",'
            '"costime": "$request_time",'
            '"realtime": "$upstream_response_time",'
            '"status": $status,'
            '"x_forwarded": "$http_x_forwarded_for",'
            '"referer": "$http_referer",'
            '"request": "$request",'
            '"upstr_addr": "$upstream_addr",'
            '"bytes":$body_bytes_sent,'
            '"dm":$request_body,'
            '"agent": "$http_user_agent" }';

   access_log /var/log/nginx/access.log main;

2. Comparison of effects before and after setting

1. JSON format of sending request

{
  "results":[
    {
   "price":{
        "pricePerMessage":0.01,
        "currency":"aaa"
      },
      "messageId":"ff4804ef-6ab6-4abd-984d-ab3b1387e852",
      "to":"385981178",
      "sentAt":"2015-02-12T09:58:20.323+0100",
      "doneAt":"2015-02-12T09:58:20.337+0100",
      "smsCount":1,
      "status":{
        "id":5,
        "groupId":3,
        "groupName":"DELIVERED",
        "name":"DELIVERED_TO_HANDSET",
        "description":"Message delivered to handset"
      },
      "error":{
        "groupId":0,
        "groupName":"OK",
        "id":0,
        "name":"NO_ERROR",
        "description":"No Error",
        "permanent":false
      }
    }
  ]
}

2. The result of nginx printout before setting the post request obviously does not print out the parameters

3. After setting the post request, nginx prints the parameters and finds that the parameters are printed but there are garbled characters

4. Solve the garbled code

(1) You need to add escape=json after log_format main

(2) The nginx version needs to be 1.15.1 or above

(3) Satisfy the final printed results above

3. Necessity of viewing nginx logs

1. More comprehensive: It can record visitor's IP browser and request parameters and other information more comprehensively than Java request log;

2. When the number of requests is large, the Java program cannot handle it. At this time, the Java log does not have redundant access requests, but nginx does, which is helpful for troubleshooting and optimizing system performance.

【Summarize】

1. Problems can be quickly located through logs;

2. When troubleshooting a problem, you need to consider it comprehensively and from multiple dimensions.

This is the end of this article about how to set Nginx log printing post request parameters. For more relevant Nginx log printing post request parameters, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of nginx log configuration instructions
  • Detailed steps to enable Nginx to view access logs in real time
  • Detailed explanation of Nginx log cutting by date (cutting by day)
  • Detailed analysis and configuration of access_log in nginx server
  • Configuration example of logging in JSON format in nginx
  • Add request response log to nginx log (recommended)
  • Nginx turns off favicon.ico and robots.txt logging configuration

<<:  Brief Analysis of MySQL B-Tree Index

>>:  Example of implementing element table row and column dragging

Recommend

Implementing shopping cart function based on vuex

This article example shares the specific code of ...

A brief discussion on the role of Vue3 defineComponent

Table of contents defineComponent overload functi...

MySQL fuzzy query usage (regular, wildcard, built-in function)

Table of contents 1. MySQL wildcard fuzzy query (...

Nginx URL rewriting mechanism principle and usage examples

URL rewriting helps determine the preferred domai...

MySQL SQL statement analysis and query optimization detailed explanation

How to obtain SQL statements with performance iss...

A brief analysis of event bubbling and event capture in js

Table of contents 01-Event Bubbling 1.1- Introduc...

Drawing fireworks effect of 2021 based on JS with source code download

This work uses the knowledge of front-end develop...

Negative distance (empathy) - iterative process of mutual influence

Negative distance refers to empathy. Preface (rai...

A good way to improve your design skills

So-called talent (left brain and right brain) Tha...

Detailed explanation of HTML onfocus gain focus and onblur lose focus events

HTML onfocus Event Attributes Definition and Usag...

How to use ss command instead of netstat in Linux operation and maintenance

Preface When operating and managing Linux servers...

How to skip errors in mysql master-slave replication

1. Traditional binlog master-slave replication, s...