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
+38.2k Golang : UDP client server read write example
+16.2k Golang : How to remove certain lines from a file
+33.8k Golang : Save image to PNG, JPEG or GIF format.
+4.3k Gogland : Datasource explorer
+6k Golang : Get Alexa ranking data example
+3.9k Unix/Linux : How to pipe/save output of a command to file?
+13.4k Golang : package is not in GOROOT during compilation
+9.3k Generate Random number with math/rand in Go
+5.2k Golang : Detect face in uploaded photo like GPlus
+6.8k Golang : Get today's weekday name and calculate target day distance example
+8.6k Golang : Resumable upload to Google Drive(RESTful) example
+12.8k How to automatically restart your crashed Golang server