How to intercept specific user agent in Nginx

WBOY
Release: 2023-05-13 10:25:05
forward
1220 people have browsed it

Blacklist specific user agents in nginx

To configure the user agent blocking list, open your website's nginx configuration file and find the server definitions section. This file may be placed in different places, depending on your nginx configuration or linux version (e.g., /etc/nginx/nginx.conf, /etc/nginx/sites-enabled/,/usr /local/nginx/conf/nginx.conf,/etc/nginx/conf.d/).

Copy code The code is as follows:

server {
listen 80 default_server;
server_name xmodulo.com;
root /usr/share/nginx/html;
....
}

After opening the configuration file and finding the server section, add the following if statement somewhere within that section.

Copy code The code is as follows:

server {
listen 80 default_server;
server_name xmodulo.com;
root /usr/share/nginx/html;
# Case-sensitive matching
if ($http_user_agent ~ (antivirx|arian) {
return 403;
}

Case-independent matching

Copy code The code is as follows:

if ($http_user_agent ~* (netcrawl|npbot|malicious)) {
                  return 403; #As you can imagine, these if statements use regular expressions to match any bad user string and return a 403 http status code to the matched object. $http_user_agent is a variable in the http request that contains the user agent string.' The ~' operator does a case-sensitive match against the user-agent string, while the '~*' operator does a case-insensitive match. The '|' operator is a logical OR, so you can put numerous User agent keyword and then block them all.

After modifying the configuration file, you must reload nginx to activate blocking:

 $ sudo /path/to/nginx -s reload
Copy after login
Copy after login

You can do this by using the command with "--user -agent" option of wget tests user agent blocking.

 $ wget --user-agent "malicious bot" http://<nginx-ip-address>
Copy after login

Manage user agent blacklist in nginx

How to intercept specific user agent in Nginx

So far, I have shown how to block http requests for some user agents in nginx. What if you have many different types of web crawler bots to block?

Since the user agent blacklist will grow very large large, so putting them in the server part of nginx is not a good idea. Instead, you can create a separate file in which you list all blocked user agents. For example, let's create /etc/nginx /useragent.rules and define a map that defines all blocked user agents in the following format.

  $ sudo vi /etc/nginx/useragent.rules
Copy after login

Copy the code The code is as follows:

map $http_user_agent $badagent {

                                   ;

                                                                                                                                                                                                                                                                

Similar to the previous configuration, '~*' will match keywords in a case-insensitive manner, while '~' will match keywords using a case-sensitive regular expression. The "default 0" line means that any user agents not listed in other files will be allowed.

Next, open the nginx configuration file of your website, find the section containing http, and then add the following line somewhere in the http section.

Copy code code as follows:

Http {
.....
Include /etc/nginx/useraquest.rules
}

## 注意 Note , the include statement must appear before the server section (that's why we added it to the http section).

Now, open the nginx configuration section that defines your server and add the following if statement:

Copy the code The code is as follows:

server {

     ....
                                                                                                                                                                                .

 $ sudo /path/to/nginx -s reload
Copy after login
Copy after login

Now, any user agent containing the keywords listed in /etc/nginx/useragent.rules will be automatically banned by nginx.

The above is the detailed content of How to intercept specific user agent in Nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!