Block bots from directory

Hello, I know I can use robots.txt but I'd like to mess with a more immediate solution. I tried something like this, but then when I went to view it in browser, it gave 404 so I think my code is wrong. I placed this in the server block in /etc/nginx/sites-enabled/example.com.

# Block some bots from everything in this directory
location = /schedule/ {
    if ($http_user_agent ~* "Googlebot|bingbot") {
         return 403;
    } 
} 

1 Reply

It's probably not working because you have quotes around the things you want to block

Try

if ($http_user_agent ~* (googlebot|bingbot) ) {
    return 403;
}

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct