Blocking by user agent if ip doesn't match

karabaja nginx-forum at
Tue May 31 22:49:40 MSD 2011

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
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;

But I don't know how to add ip regex match to that and what end result
would be. Or negative match in this case I guess.
It is something like this for htaccess !^66.*$ to match those ips that
don't start with 66. for example. But again I am not sure what the right
syntax would be in nginx config or if it is even possible to match both

Posted at Nginx Forum:,202715,202715#msg-202715

More information about the nginx mailing list