
Nginx logs are needed to track all events generated on the Webserver. However there are some specific events which are not needed to track. turning off of those logs or events for specific directive or location will give a performance boost to the webserver by reducing I/O and CPU time.
On a wordpress website, if we load home|default page or any other page, it will unnecessary log page load information of css|html|js|php files and some other files. This generally increases log file size and also it increases difficulty to analyze the weserver logs. This article contains precise information on how to disable nginx logging for a specific location, specific type of files and IP addresses.
Topic
- How to configure nginx webserver to turn off log for specific location or directive?
- How to turn off logging for specific IP address or location on Nginx?
- Disable nginx logs for specific IP and locations
- Exclude nginx logs for specific IP and locations
apt
- Linux
- Windows
- Nginx
Solution
Following are the sample examples describe about several log exclusion scenarios.
Turn off access log if IP addresses are 192.168.0.1, 192.168.0.2 and 192.168.0.3
location / {
if ($remote_addr = "192.168.0.1") {
access_log off;
}
if ($remote_addr = "192.168.0.2") {
access_log off;
}
if ($remote_addr = "192.168.0.3") {
access_log off;
}
}
Turn off access log if location is "/static/images"
location ~* ^/static/images/* {
access_log off;
log_not_found off;
expires max;
}
Turn off access logs for images, videos, zip files, etc.
location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|
rss|atom|js|jpgeg|gif|png|ico|zip|tgz|gz|rar|
bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off;
expires max;
log_not_found off;
}
Don’t log css and js files
location ~*.(css|js)$ {
access_log off;
log_not_found off;
}
Deny public access to wp-config.php
location ~* wp-config.php {
deny all;
}
Deny upload permission if content types are other than images, videos, music
location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php|js|swf)$ {
deny all;
}
Complete configuration file (nginx.conf)
server {
listen 80 default_server;
server_name _;
access_log /var/log/nginx/access.log main;
root /websites/nginx/webroot/default_site;
index index.html;
location / {
# turn off access log for IP address
if ($remote_addr = "192.168.0.1") {
access_log off;
}
if ($remote_addr = "192.168.0.2") {
access_log off;
}
if ($remote_addr = "192.168.0.3") {
access_log off;
}
# Turn off access log if location is "/static/images"
location ~* ^/static/images/* {
access_log off;
log_not_found off;
expires max;
}
# Turn off access logs for images, videos, zip files, etc.
location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|
rss|atom|js|jpgeg|gif|png|ico|zip|tgz|gz|
rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off;
expires max;
log_not_found off;
}
# Don't log css and js files
location ~*.(css|js)$ {
access_log off;
log_not_found off;
}
# Deny public access to wp-config.php
location ~* wp-config.php {
deny all;
}
# Deny upload permission if content types are other than images, videos, music
location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php|js|swf)$ {
deny all;
}
}