Nginx : How to block user agent ?
Problem : Looking for ways to configure Nginx to block certain user agents like crawlers, SEO tools and robots from molesting your website.
Solution : Configure nginx.conf file to instruct Nginx to return 403 status when certain user agents visit the website.
For example:
vi /usr/local/nginx/conf/nginx.conf
and add these lines under the server block. If you have additional server block for SSL, add the same lines for the server block as well.
server {
listen 80;
if ($http_user_agent ~* (wget|majestic|ahrefs) ) {
return 403;
}
Note : ~* means case insensitive blocking. For case sensitive, use ~ instead.
restart nginx
/usr/local/nginx/sbin/nginx -s reload
In this way, whenever one of these user agents visit your website, it will be given a 403 status.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.1k Golang : Defer function inside init()
+7.1k Golang : Auto-generate reply email with text/template package
+4.4k PHP : Hide PHP version information from curl
+9.5k Golang : Roll the dice example
+6.3k Golang : Getting Echo framework StartAutoTLS to work
+19.2k Golang : Saving private and public key to files
+5.6k Golang : Spell checking with ispell example
+18.1k Golang : How to run your code only once with sync.Once object
+10.4k Golang : Generate DSA private, public key and PEM files example
+11.4k Elastic Search : Return all records (higher than default 10)
+12.8k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+13.5k Golang : Execute function at intervals or after some delay