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
+10.6k Golang : Meaning of omitempty in struct's field tag
+9k Golang : Executing and evaluating nested loop in html template
+28.3k Golang : Move file to another directory
+6.4k Golang : Extract XML attribute data with attr field tag example
+14.4k Golang : Convert IP version 6 address to integer or decimal number
+31.8k Golang : bufio.NewReader.ReadLine to read file line by line
+10.7k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+17k Golang : read gzipped http response
+14.6k Golang : Parsing or breaking down URL
+17.4k Golang : When to use init() function?
+8.6k PHP : How to parse ElasticSearch JSON ?
+24.8k Golang : How to validate URL the right way