Nginx : Block User Agent

In Nginx, you can block certain user agents (normally it is crawler) like this :

/etc/nginx/sites-enabled/default

server {
  listen          80;
  server_name     mysite.com;
  root            /etc/tomcat7/webapps/mysite;

  if ($http_user_agent ~* (ahrefs|wget|crawler|majestic) ) {
    return 403;
  }

  location / {
        <!-- xxx -->
  }
}

In above example, for “user agent” that contains one of this pattern : ahrefs|wget|crawler|majestic, HTTP 403 will be returned.

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments