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
+8k Golang : Turn string or text file into slice example
+3.9k PHP : See installed compiled-in-modules
+4.7k AWS S3 : Prevent Hotlinking policy
+4.7k Golang : Convert Chinese UTF8 characters to Pin Yin
+12.3k Golang : Check if a file exist or not
+4.8k Golang : Process non-XML/JSON formatted ASCII text file example
+28.6k Golang : How to redirect to new page with net/http?
+9.7k Golang : Post data with url.Values{}
+49.6k Golang : Check if item is in slice/array
+8.1k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+14.9k CodeIgniter/PHP : Create directory if does not exist example
+21.6k Golang : Daemonizing a simple web server process example