Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow only one User-Agent, block the rest in nginx?

Tags:

nginx

new to this site so i'll keep it brief:

I have currently:

    if ($http_user_agent ~* (A-certain-self-made-User-Agent-here)) {
            return 200;
    }

Which works very well. (Tested by switching 200 to 403).

My question is: Is there a way in: /etc/nginx/sites-enabled/default to make it allow only ONE User-Agent and deny the rest?

I know this seems stupid, but it's something i'd like to have done, (if possible). Like maybe this?:

    if (http_user_agent ~*(user-agent)) {
           return 200;
    else
           return 403;
    }
like image 528
Neil Mcdonald Avatar asked Jul 16 '13 10:07

Neil Mcdonald


People also ask

How do I whitelist user agent?

Click on 'Add exception' and the Whitelist Rules for Bot Access window will pop up. Under the dropdown menu, select 'User agent' and key in 'AhrefsBot' and 'AhrefsSiteAudit' then click 'Confirm'.

How to block user agent in nginx?

To configure the user-agent block list, open the nginx configuration file for your website. This file can be found in different places depending on your nginx setup or Linux distribution (e.g.: /etc/nginx/nginx. conf, /etc/nginx/sites-enabled/<your-site>, /usr/local/nginx/conf/nginx. conf, /etc/nginx/conf.

What is Http_user_agent?

It's just a string that a browser optionally sends to the server. Firefox can let you change the string entirely via the about:config page by modifying/creating the string general. useragent.


2 Answers

Try this:

if ($http_user_agent !~* (A-certain-self-made-User-Agent-here)) {
        return 403;
}

This should be a 'not match' on your certain user agent. Reference info here: HttpRewriteModule

like image 145
ProfessionalAmateur Avatar answered Sep 22 '22 13:09

ProfessionalAmateur


if ($http_user_agent !~* "A-certain-self-made-User-Agent-here") {
    return 403;
}

is working on my site.

like image 25
Vidyut Avatar answered Sep 22 '22 13:09

Vidyut