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

CSS3 transition to implement notification message carousel

Vue version, copy it to the file and use it <t...

CentOS7 configuration Alibaba Cloud yum source method code

Open the centos yum folder Enter the command cd /...

js to realize the production method of carousel

This article shares the specific code for js to r...

MySQL 8.0 WITH query details

Table of contents Learning about WITH queries in ...

How to optimize MySQL indexes

1. How MySQL uses indexes Indexes are used to qui...

A Brief Analysis of MySQL PHP Syntax

Let's first look at the basic syntax of the c...

Sample code for configuring nginx to support https

1. Introduction Are you still leaving your websit...

Resolving MySQL implicit conversion issues

1. Problem Description root@mysqldb 22:12: [xucl]...

Description of the execution mechanisms of static pages and dynamic pages

1. A static page means that there are only HTML ta...

How to turn local variables into global variables in JavaScript

First we need to know the self-calling of the fun...

Specific steps for Vue browser to return monitoring

Preface When sharing a page, you hope to click th...

Solution to the error when calling yum in docker container

When executing yum in dockerfile or in the contai...

The implementation process of ECharts multi-chart linkage function

When there is a lot of data to be displayed, the ...