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
+7.8k Golang : Find duplicate files with filepath.Walk
+8.5k Golang : Get all countries currencies code in JSON format
+10.4k Golang : Byte format example
+24.6k Golang : How to write CSV data to file
+16.7k Golang : Get all upper case or lower case characters from string example
+14.8k Golang : Generate universally unique identifier(UUID) example
+21.2k Golang : Read a file into an array or slice example
+19.3k Golang : Underscore or snake_case to camel case example
+7.1k Golang : Multiplexer with net/http and map
+7.9k Golang : Capture text return from exec function example
+7.2k Golang : Emulate NumPy way of creating matrix example