Nginx : Block User Agent

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


server {
  listen          80;
  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.

About the Author

author image
Founder of, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.