NGINX web server allows you to exclude requests from NGINX log. By default, NGINX will log every request that it receives in its access-log file. But sometimes you may not want to record certain requests & URLs in your log files. This happens if there are too many log entries and you want to make it easier to debug issues by removing some of the clutter in log files. In this article, we will learn how to exclude requests from NGINX log.
How to Exclude Requests from NGINX Log
NGINX allows you to exclude log entries in different ways – by URLs or by IP. We will look at each of these cases one by one.
Exclude Specific URLs
Often developers don’t want to log entries about static files such as images that generate too many log entries. In such cases, open NGINX configuration file in a text editor.
$ vi /etc/nginx/nginx.conf
Add the following lines to turn off logging for image files, documents, font files, icons, & spreadsheets. You can customize them as per your requirements.
location ~* \.(png|jpg|jpeg|gif|ico|woff|otf|ttf|eot|svg|txt|pdf|docx?|xlsx?)$ { access_log off; log_not_found off; expires max; add_header Pragma public; add_header Cache-Control "public"; add_header Vary "Accept-Encoding"; }
In the above code, we turn off logging using access_log directive. We also turn off error logging using log_not_found. But we set its cache headers and send them in response so that caching continues to work, while logging is off.
Similarly, if you want to turn off logging for specific URL, you can add ‘access_log off’ in the location block for that URL.
location /test.html { access_log off; }
Alternatively, if you want to turn of logging for all requests to a specific folder /static/, add ‘access_log off’ in location block of that folder.
location /stats/ { try_files $uri $uri/ =404; access_log off; }
Save and close the file.
Restart NGINX server to apply changes.
$ sudo service nginx restart
Exclude Specific IP addresses
Sometimes you may want to exclude certain IP addresses such as localhost 127.0.0.1 or testing server’s IP addresses from being logged. If so, then open NGINX configuration file and make a list of all IP addresses that you want to ignore logging. You can customize this list as per your requirement.
map $remote_addr $log_ip { "127.0.0.1" 0; "10.0.0.2" 0; "10.0.0.3" 0; default 1; }
Then in server block of NGINX configuration file, add the following line.
server { […] access_log /var/log/nginx/access.log main if=$log_ip; }
In the above code, requests from IP addresses 127.0.0.1, 10.0.0.2, 10.0.0.3. For these IP addresses, $log_ip value is 0. Therefore, if condition is not satisfied and logging does not happen. For other IP addresses, $log_ip is 1 and logging happens. NGINX>=1.7.0 allows you to enable conditional logging using if statement.
In this article, we have learnt how to disable logging in NGINX for specific requests & IP addresses. You can use it to easily ignore specific request URLs and IP addresses from being logged. This removes unnecessary clutter from your log files and makes it easy to debug issues.
Also read:
How to Pass Variable in cURL Command
Shell Script to Restart Service If Not Running
Shell Script to Read Data From Text File
Awk Split One Column into Multiple Columns
How to Read Variable from File in Shell Script
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.