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;
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Setting up Python Interpreter and running Python Code on Docker Container

Doing CRUD: terraform Style

Day 04 — Advent of Code 2018

WORKBOX, Teambox, Dropbox… What does the “box” stand for?

The Top 10 Open Source Tools Of 2021, According To The Experts 2021

Token to Full Code Repository access

How-to use Deployment slots with Azure Functions.

Deployment slots with Azure Functions

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
adil

adil

More from Medium

How Docker Container Works?

Access blocked by CORS Policy: What’s CORS and what should you do?

Communication in Containers

How to implement logging in your REST service by using Elasticsearch

drawing