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
+8.9k Golang : Setting variable value with ldflags
+5.1k AWS S3 : Prevent Hotlinking policy
+16.1k Golang : When to use init() function?
+29.3k Golang : Download file example
+6.5k Golang : Detect sample rate, channels or latency with PortAudio
+8.6k Golang : Populate slice with sequential integers example
+5k Java : Human readable password generator
+6.3k Golang : Dealing with struct's private part
+28k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+14.9k Golang : Find out mime type from bytes in buffer
+6.9k Golang : Regular Expression find string example
+7.9k Golang : Intercept and compare HTTP response code example