Blocking by user agent if ip doesn't match

António P. P. Almeida appa at
Tue May 31 23:04:31 MSD 2011

On 31 Mai 2011 19h49 WEST, nginx-forum at wrote:

> Hello everyone. Sorry for double posting this question in How to
> section of the forum but I've noticed later there is a lot of
> un-replied threads there and that mailing list is more active so I
> am assuming I have more chance of getting some help here.
> I am hoping this is possible and I'd really appreciate some help on
> configuring it.  We are having some bots on our site that are using
> google spider user agent but they are fake and their ip range has
> nothing to do with Google.  So I am looking for some solution that
> would match visitors with user agent is Google who's ip doesn't
> start with for example 66.x or 70.x and block them from accessing.
> I've found this example on one site
> if ($http_user_agent ~ (Purebot|Lipperhey|MaMa
> CaSpEr|libwww-perl|Mail.Ru|gold crawler) ) {
> return 403;
> }

Try this:

if ($http_user_agent ~* "Google Bot") {
  allow 66.x;
  allow 70.x;
  deny all;

--- appa

More information about the nginx mailing list