nginx log post data

How to Log POST data in NGINX

By default, NGINX does not log POST data since it can be bulky and take up disk space. However, sometimes you may need to log POST data for debugging purposes. In this article, we will look at how to log POST data in NGINX server.


How to Log POST data in NGINX

Here are the steps to log POST data in NGINX.


1. Open NGINX configuration file

Open terminal and run the following command to open NGINX configuration file.

$ sudo vi /etc/nginx/nginx.conf


2. Log POST data in NGINX

Add the following lines in http block of server configuration.

log_format post_logs '[$time_local] "$request" $status '  

                     '$body_bytes_sent "$http_referer" '        

                     '"$http_user_agent" [$request_body]';

access_log  /var/log/nginx/postdata-access.log post_logs;

In the above code, we log request time, status, bytes sent, referrer, user agent and body in access log at /var/log/nginx/postdata-access.log file. The log_format command is used to override the default log format used by NGINX.


3. Restart NGINX Server

Restart NGINX Server to apply changes.

$ sudo service nginx restart

That’s it. Now NGINX will automatically log POST data. You can view this information by simply opening the log file with the following command which shows the most recent 10 POST requests.

$ sudo tail -n 10 /var/log/nginx/postdata-acces.log | grep POST

If you want to view the full log file then use the following command.

$ sudo cat /var/log/nginx/postdata-access.log |less

Also read :

How to Read POST data in NGINX
How to Setup Catch-All subdomains in NGINX
How to Uninstall NodeJS, NPM in Ubuntu
How to Setup Error Reporting in PHP
How to Redirect URL/Page with Anchor Text

Leave a Reply

Your email address will not be published. Required fields are marked *