How to disallow some referrers and allow some user agents on Nginx?

Photo by Francisco Galarza on Unsplash
map $http_referer $referrer_check { 
default 1;
"~google.com" 0;
"*.google.com" 0;
}

map $http_user_agent $mobile_check {
default 1;
~(Android|Darwin) 0;
}
set $flag "0";if ($referrer_check) {
set $flag "1";
}
if ($mobile_check) {
set $blockit $flag;
}
if ($blockit) {
return 400;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store