Detect Googlebot visits using nginx - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

Detect Googlebot visits using nginx
===================================

### [\#Nginx](https://rocketeersapp.com/knowledge/nginx)

How to detect when and how often Googlebot visits your website using a few configuration lines in nginx.

 Published by [Mark van Eijk](https://rocketeersapp.com/author/mark-van-eijk) on November 18, 2024 · 1 minute read

1. [Detecting Googlebot](#content-detecting-googlebot)
2. [Logging the requests](#content-logging-the-requests)

[\#](#content-detecting-googlebot "Permalink")Detecting Googlebot
-----------------------------------------------------------------

It's very easy to detect the Googlebut using the user agent in nginx, here we use the `map` directive to set the variable `$googlebot` to `yes` or keep it empty depending on the given user agent:

 ```
map $http_user_agent $googlebot {
    default "";
    "~*googlebot" "yes";
}

```

[\#](#content-logging-the-requests "Permalink")Logging the requests
-------------------------------------------------------------------

When `$googlebot` is filled, we want to log the request in a log file. This can be done using the `access_log` directive:

 ```
access_log /var/www/logs/googlebot.log bots if=$googlebot;

```

That's it!

You can read here [how you can log multiple bots](/log-bot-requests-nginx) how to log multiple bots for your virtual host in nginx.

### Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#Nginx](https://rocketeersapp.com/knowledge/nginx)

- [Server Side Includes (SSI) in nginx](https://rocketeersapp.com/knowledge/server-side-includes-ssi-nginx)
- [Configure Content Security Policy with nonce using nginx](https://rocketeersapp.com/knowledge/content-security-policy)
- [nginx rewrite or internal redirection cycle](https://rocketeersapp.com/knowledge/nginx-rewrite-or-internal-redirection-cycle)
- [nginx: upstream sent too big header](https://rocketeersapp.com/knowledge/nginx-upstream-sent-too-big-header)
- [How to install Nginx on Ubuntu](https://rocketeersapp.com/knowledge/how-to-install-nginx)
- [Use nginx try\_files to make your site static](https://rocketeersapp.com/knowledge/nginx-try-files)

 [View all 7 articles →](https://rocketeersapp.com/knowledge/nginx)
