3

I have my nginx setup to block certain referers like so:

if ($http_referer ~* (site_name) ) {
  return 403;
}

This works fine, however I'd like to also log the blocked referrer to a separate file.

I tried adding

 access_log /path/to/server/bad_domain.log;

in the if statement; however this doesn't work.

Any ideas?

Thanks.

Edit:

I've also tried this to no avail.

if ($http_referer ~* (site_name) ) {
  set $crawler 'yes';
  return 403;


}

location ~ .* {
  if ($crawler = 'yes') {
  access_log /path/to/server/bad_domain.log;
    }
}

Edit 2:

Trying

map $http_referer $log_referer {
  domain1.com  1;
  default      0;
}

server { ..
    if ($http_referer = "1") {
    set $log_referer 1;
}
   access_log /path/to/logs/bad_domain.log if=$log_referer;

...}

Gives me the output of

nginx: [emerg] unknown log format "if=$log_referer"
1

According to nginx documentation found here and here, try to use map rule to map your http referer to certain value and then log to specific file according that value. Put this map in your http context (outside the server context):

map $http_referer $log_referer {
  example.com  1;
  default      0;
}

This goes to your server, location, etc.;

access_log /path/to/bad_domain.log combined if=$log_referer;
  • Hi, I tried your method (see edit) but it gives me an error. Any idea? Thanks. – Jone Does Aug 26 '15 at 22:01
  • Hi, I edited my reply, now it works. Sorry, my bad. – BackSlash Aug 26 '15 at 22:37
  • Unfortunately that's what I had and it was still causing the error. I even tried moving the map to nginx.conf which again resulted in the same error. Thanks. – Jone Does Aug 26 '15 at 22:54
1

Your second try[Edit 2] is having the correct syntax but I think the error occurs as you are using an older version of NGINX. The 'if' in access log was introduced in NGINX 1.7, see the release notes.

If you have installed NGINX from Ubuntu repo then they are shipping ver 1.4.x . Run nginx with -V to know which version you are running.

$ nginx -V                                                                                         
nginx version: nginx/1.8.0   

I have setup the NGINX configuration at the following bash fiddle :

https://thinapps.org/fiddle/lwcorkai27030iawscqklwonderquest

Have a look! to know how to pull the latest version of NGINX and work with conditional logging.

  • Hi, thank you I updated to 1.8.0 through the stable ppa nginx -v nginx version: nginx/1.8.0 and copied the config into the nginx.conf, however on restart nginx -t still shows nginx: [emerg] unknown log format "if=$log_referer" in /etc/nginx/nginx.conf:47 Any idea's? Thanks. – Jone Does Aug 27 '15 at 11:04
0

I faced the same issue and solved by adding combined to the access_log

access_log /path/to/logs/bad_domain.log combined if=$log_referer;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.