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
+21.4k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+8.9k Golang : Create and shuffle deck of cards example
+18.3k Golang : Implement getters and setters
+9.2k Golang : Extract or copy items from map based on value
+6.8k Golang : Get Alexa ranking data example
+5.6k Golang : Find change in a combination of coins example
+7.9k Golang : How To Use Panic and Recover
+19.8k Golang : Check if os.Stdin input data is piped or from terminal
+12.2k Golang : Exit, terminating or aborting a program
+35.7k Golang : Converting a negative number to positive number
+9.2k Golang : Get all countries currencies code in JSON format
+16.5k Golang : Get own process identifier