Reputation: 7887
I'm trying to "ignore" or redirect certain request in the nginx log based on the request URI.
So far I have this:
server {
listen 80;
#...
access_log /var/log/nginx/site.access;
error_log /var/log/nginx/site.error info;
#...
try_files $uri @app
location = /lbcheck.html {
access_log off;
log_not_found off;
try_files $uri @app;
}
location @app {
proxy_redirect off;
proxy_pass http://unix:/tmp/site.unicorn.sock;
#...
}
}
Basically I want the proxied @app to respond to the request for /lbcheck.html
but I don't want it logged in /var/log/nginx/site.access;
I know i can do it with a if ($uri = /lbcheck.html) { access_log off; }
in location @app
but if's are evil, so I don't want to use them.
Upvotes: 17
Views: 26299
Reputation: 87
Use this script and put this in the Nginix Configuration file. You must have a knowledge in Nginix configuration in order to understand this.
Specific file
map $request $DisableFileNameLogging{
~*filename1.php 0;
~*filename2.php 0;
~*filename3.php 0;
default 1;
}
server{
access_log /www/wwwlogs/filename.log combined if=$DisableFileNameLogging;
}
map variable should be outside the server. Do not forget to add combined keyword. In ~*filename1.php 0; # 0 means disabled or off, 1 means enabled or on
Put 1 if you want specific file to enable logging in the map $request $DisableFileNameLogging
For Specific IP Address:
map $request $DisableIPLogging{
0.0.0.0 0;
127.0.0.0 0;
198.168.0.1 0;
default 1;
}
server{
access_log /www/wwwlogs/filename.log combined if=$DisableIPLogging;
}
Combination of Specific File and Specific IP Address
map $request $DisableFileNameLogging{
~*filename1.php 0;
~*filename2.php 0;
~*filename3.php 0;
default 1;
}
map $request $DisableIPLogging{
0.0.0.0 0;
127.0.0.0 0;
198.168.0.1 0;
default 1;
}
server{
set $DisableLogCond1 1;
if ($DisableFileNameLogging = 0){
set $DisableLogCond1 0;
}
if ($DisableIPLogging = 0){
set $DisableLogCond1 0;
}
access_log /www/wwwlogs/test_bwa.bookingwebapp.com.log combined if=$DisableLogCond1;
}
Upvotes: 0
Reputation: 22841
You can do this with conditional logging. It would look something like
map $request $loggable {
~*lbcheck\.html 0;
default 1;
}
access_log /path/logs/name.log if=$loggable;
Upvotes: 28
Reputation: 25701
I'm not sure if there is a way of doing it just within Nginx's configuration syntax, I just tried and it didn't seem possible without an if statement.
However I do know that doing it in the way you're trying to is against Nginx's philosophy of how to write config files.
Instead of writing complicated config rules, you should be generating your nginx.conf files with whatever configuration tool you prefer, so that the actual config file is simple, with all the complicated bits generated for you by your tool.
e.g. your source file that your nginx.conf is generated from should look something like:
%START_APP_CONFIG%
proxy_redirect off;
proxy_pass http://unix:/tmp/site.unicorn.sock;
%END_APP_CONFIG%
location = /lbcheck.html {
access_log off;
%INSERT_APP_CONFIG%
}
location @app {
%INSERT_APP_CONFIG%
}
Although this may seem like a lot of work to just be able to tweak a single config variable, it actually makes using config files much more pleasant in the long (and medium) term as you will always be generating config files that are easy to read and understand, rather than having complicated conditions in them.
Upvotes: 7