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

new to this site, so I will keep him posted:

I have:

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: / etc / nginx / sites-enabled / default so that it only allows ONE User-Agent and forbids the rest

I know this seems silly, but this is what I would like to do (if possible). How maybe this ?:

  if (http_user_agent ~*(user-agent)) { return 200; else return 403; } 
+6
source share
3 answers

Try the following:

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

This must be a "do not match" with your specific user agent. Background information here: HttpRewriteModule

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

works on my site.

+4
source

You can try the following in the right place.

 set $isallowed = ""; if ($http_user_agent = allowed_user_agent) { set $isallowed "${isallowed}YES"; } if ($isallowed !~ YES) { rewrite ^ http://yourserver.com permanent; } 
0
source

Source: https://habr.com/ru/post/949552/


All Articles