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.2k Golang : Compare files modify date example
+18.6k Golang : Example for RSA package functions
+16.4k Golang : How to extract links from web page ?
+7.9k Golang : Generate human readable password
+11k Golang : Sieve of Eratosthenes algorithm
+31.9k Golang : How to convert(cast) string to IP address?
+30.6k Golang : How to verify uploaded file is image or allowed file types
+29.5k Golang : JQuery AJAX post data to server and send data back to client example
+13.1k Python : Convert IPv6 address to decimal and back to IPv6
+9.6k Golang : Qt Yes No and Quit message box example
+9.9k Golang : Resumable upload to Google Drive(RESTful) example
+29.7k Golang : Saving(serializing) and reading file with GOB