Disable logging for specific useragent in nginx conf
up vote
1
down vote
favorite
I want to disable logging for a specific useragent. This is a part of my current conf-file.
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
I've tried adding access_log off; but get the following error:
nginx: [emerg] "access_log" directive is not allowed here
I'm assuming this is because I only have a server block. I need a location block also. I've tried the following code:
location / {
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
}
But I get the following error:
duplicate location "/"
In my conf-file I already have this code:
location / {}
try_files $uri $uri/ =404;
Can I combine the two location snippets into one? Or how do I proceed?
nginx
|
show 1 more comment
up vote
1
down vote
favorite
I want to disable logging for a specific useragent. This is a part of my current conf-file.
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
I've tried adding access_log off; but get the following error:
nginx: [emerg] "access_log" directive is not allowed here
I'm assuming this is because I only have a server block. I need a location block also. I've tried the following code:
location / {
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
}
But I get the following error:
duplicate location "/"
In my conf-file I already have this code:
location / {}
try_files $uri $uri/ =404;
Can I combine the two location snippets into one? Or how do I proceed?
nginx
Theaccess_logdirective includes anif=conditionwhich can be controlled by amap. There's an example here
– Richard Smith
Nov 8 at 19:25
Thanks! Is there a way to also return 403 if condition=true?
– Niska
Nov 8 at 20:10
Yes. Use:if ($condition) { return 403; }
– Richard Smith
Nov 8 at 20:36
Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
– Niska
Nov 8 at 20:44
No. Negate themapand useif ($condition = 0) { return 403; }. (assuming you're using1and0in yourmap)
– Richard Smith
Nov 8 at 20:48
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to disable logging for a specific useragent. This is a part of my current conf-file.
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
I've tried adding access_log off; but get the following error:
nginx: [emerg] "access_log" directive is not allowed here
I'm assuming this is because I only have a server block. I need a location block also. I've tried the following code:
location / {
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
}
But I get the following error:
duplicate location "/"
In my conf-file I already have this code:
location / {}
try_files $uri $uri/ =404;
Can I combine the two location snippets into one? Or how do I proceed?
nginx
I want to disable logging for a specific useragent. This is a part of my current conf-file.
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
I've tried adding access_log off; but get the following error:
nginx: [emerg] "access_log" directive is not allowed here
I'm assuming this is because I only have a server block. I need a location block also. I've tried the following code:
location / {
if ($http_user_agent ~ (bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) ) {
return 403;
}
}
But I get the following error:
duplicate location "/"
In my conf-file I already have this code:
location / {}
try_files $uri $uri/ =404;
Can I combine the two location snippets into one? Or how do I proceed?
nginx
nginx
asked Nov 8 at 19:11
Niska
73
73
Theaccess_logdirective includes anif=conditionwhich can be controlled by amap. There's an example here
– Richard Smith
Nov 8 at 19:25
Thanks! Is there a way to also return 403 if condition=true?
– Niska
Nov 8 at 20:10
Yes. Use:if ($condition) { return 403; }
– Richard Smith
Nov 8 at 20:36
Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
– Niska
Nov 8 at 20:44
No. Negate themapand useif ($condition = 0) { return 403; }. (assuming you're using1and0in yourmap)
– Richard Smith
Nov 8 at 20:48
|
show 1 more comment
Theaccess_logdirective includes anif=conditionwhich can be controlled by amap. There's an example here
– Richard Smith
Nov 8 at 19:25
Thanks! Is there a way to also return 403 if condition=true?
– Niska
Nov 8 at 20:10
Yes. Use:if ($condition) { return 403; }
– Richard Smith
Nov 8 at 20:36
Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
– Niska
Nov 8 at 20:44
No. Negate themapand useif ($condition = 0) { return 403; }. (assuming you're using1and0in yourmap)
– Richard Smith
Nov 8 at 20:48
The
access_log directive includes an if=condition which can be controlled by a map. There's an example here– Richard Smith
Nov 8 at 19:25
The
access_log directive includes an if=condition which can be controlled by a map. There's an example here– Richard Smith
Nov 8 at 19:25
Thanks! Is there a way to also return 403 if condition=true?
– Niska
Nov 8 at 20:10
Thanks! Is there a way to also return 403 if condition=true?
– Niska
Nov 8 at 20:10
Yes. Use:
if ($condition) { return 403; }– Richard Smith
Nov 8 at 20:36
Yes. Use:
if ($condition) { return 403; }– Richard Smith
Nov 8 at 20:36
Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
– Niska
Nov 8 at 20:44
Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
– Niska
Nov 8 at 20:44
No. Negate the
map and use if ($condition = 0) { return 403; }. (assuming you're using 1 and 0 in your map)– Richard Smith
Nov 8 at 20:48
No. Negate the
map and use if ($condition = 0) { return 403; }. (assuming you're using 1 and 0 in your map)– Richard Smith
Nov 8 at 20:48
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
1
down vote
As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.
For example:
map $http_user_agent $goodagent {
default 1;
~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
}
server {
access_log ... if=$goodagent;
if ($goodagent = 0) { return 403; }
...
}
The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.
add a comment |
up vote
0
down vote
At the http level declare a map like so.
map $http_user_agent $ignore_status_checks {
default 0;
"~Pingdom.*" 1;
"~*(StatusCake)" 1;
"~*mod_pagespeed*" 1;
"~*NodePing*" 1;
}
Then in your server's location block add:
if ($ignore_status_checks) {
access_log off;
}
This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.
@RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
– Shawn C.
Nov 9 at 13:54
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.
For example:
map $http_user_agent $goodagent {
default 1;
~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
}
server {
access_log ... if=$goodagent;
if ($goodagent = 0) { return 403; }
...
}
The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.
add a comment |
up vote
1
down vote
As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.
For example:
map $http_user_agent $goodagent {
default 1;
~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
}
server {
access_log ... if=$goodagent;
if ($goodagent = 0) { return 403; }
...
}
The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.
add a comment |
up vote
1
down vote
up vote
1
down vote
As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.
For example:
map $http_user_agent $goodagent {
default 1;
~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
}
server {
access_log ... if=$goodagent;
if ($goodagent = 0) { return 403; }
...
}
The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.
As your question indicates, the access_log directive cannot be used within an if block unless enclosed within a location. However, the access_log directive does include an if=condition which can be controlled by a map. There is an example at the end of this section of the manual.
For example:
map $http_user_agent $goodagent {
default 1;
~(bingbot|AhrefsBot|DotBot|Exabot|Baiduspider|SemrushBot) 0;
}
server {
access_log ... if=$goodagent;
if ($goodagent = 0) { return 403; }
...
}
The map directive must be placed outside of the server block. The access_log statement can be placed inside or outside the server block depending on whether it applies to all server blocks or just one.
answered Nov 9 at 9:00
Richard Smith
18.9k32137
18.9k32137
add a comment |
add a comment |
up vote
0
down vote
At the http level declare a map like so.
map $http_user_agent $ignore_status_checks {
default 0;
"~Pingdom.*" 1;
"~*(StatusCake)" 1;
"~*mod_pagespeed*" 1;
"~*NodePing*" 1;
}
Then in your server's location block add:
if ($ignore_status_checks) {
access_log off;
}
This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.
@RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
– Shawn C.
Nov 9 at 13:54
add a comment |
up vote
0
down vote
At the http level declare a map like so.
map $http_user_agent $ignore_status_checks {
default 0;
"~Pingdom.*" 1;
"~*(StatusCake)" 1;
"~*mod_pagespeed*" 1;
"~*NodePing*" 1;
}
Then in your server's location block add:
if ($ignore_status_checks) {
access_log off;
}
This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.
@RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
– Shawn C.
Nov 9 at 13:54
add a comment |
up vote
0
down vote
up vote
0
down vote
At the http level declare a map like so.
map $http_user_agent $ignore_status_checks {
default 0;
"~Pingdom.*" 1;
"~*(StatusCake)" 1;
"~*mod_pagespeed*" 1;
"~*NodePing*" 1;
}
Then in your server's location block add:
if ($ignore_status_checks) {
access_log off;
}
This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.
At the http level declare a map like so.
map $http_user_agent $ignore_status_checks {
default 0;
"~Pingdom.*" 1;
"~*(StatusCake)" 1;
"~*mod_pagespeed*" 1;
"~*NodePing*" 1;
}
Then in your server's location block add:
if ($ignore_status_checks) {
access_log off;
}
This will turn off the access_log for anything returns a 1 in the map. Of course, you can do want ever you want in the if.
edited Nov 9 at 14:01
answered Nov 8 at 21:29
Shawn C.
3,04111931
3,04111931
@RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
– Shawn C.
Nov 9 at 13:54
add a comment |
@RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
– Shawn C.
Nov 9 at 13:54
@RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
– Shawn C.
Nov 9 at 13:54
@RichardSmith you are 100% correct. I missed a few words in my attempt to answer prior to running to a meeting. I have updated my answer to reflect that and upvoted yours as I forgot all about the if on the access_log.
– Shawn C.
Nov 9 at 13:54
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214613%2fdisable-logging-for-specific-useragent-in-nginx-conf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The
access_logdirective includes anif=conditionwhich can be controlled by amap. There's an example here– Richard Smith
Nov 8 at 19:25
Thanks! Is there a way to also return 403 if condition=true?
– Niska
Nov 8 at 20:10
Yes. Use:
if ($condition) { return 403; }– Richard Smith
Nov 8 at 20:36
Thanks! It's working! However, I just noticed that access_log now only includes robots. I need to negate the condition so that access_log exclude robots. Is there a way to negate condition for access_log? I've tried if!=condition - but I get error...
– Niska
Nov 8 at 20:44
No. Negate the
mapand useif ($condition = 0) { return 403; }. (assuming you're using1and0in yourmap)– Richard Smith
Nov 8 at 20:48