To block a specific User-Agent, it can be implemented in Nginx, Apache, or code (such as PHP, Python). 1. In Nginx, judge $http_user_agent by if and return 403; 2. In Apache, use SetEnvIfNoCase and Deny to deny access; 3. judge User-Agent in the program and intercept the request. Common UAs that need to be blocked include python-requests, curl, empty UA, etc. Choosing the appropriate method can effectively reduce garbage traffic and security risks.
If you want to block certain User-Agents, the method is actually not complicated, it mainly depends on which environment you operate. The most common scenario is at the server level (such as Nginx, Apache) or through code control (such as PHP, Python). The following are some practical operations.
How to block User-Agent in Nginx
If you are using Nginx as a web server, you can add rules directly to the configuration file to block specific User-Agents.
if ($http_user_agent ~* (keyword|another keyword) ) { return 403; }
For example, you want to block access using "curl" or "Slurp":
if ($http_user_agent ~* (curl|Slurp) ) { return 403; }
-
~*
means case insensitive match -
return 403
is the status code that returns prohibited access - After modifying the configuration, remember to reload nginx configuration:
nginx -s reload
This method is suitable for unified management of traffic inlets, which is efficient and easy to maintain.
How to set up blocking User-Agent in Apache
If you are using Apache, you can implement similar functions through .htaccess
file or virtual host configuration.
Use SetEnvIf
and Deny
combination:
SetEnvIfNoCase User-Agent "Keyword" bad_bot Deny from env=bad_bot
For example, blocking requests from User-Agent that contain "bot" or "crawl":
SetEnvIfNoCase User-Agent "bot|crawl" bad_bot Deny from env=bad_bot
-
SetEnvIfNoCase
sets environment variables insensitively -
Deny from env=bad_bot
means that access is denied if this variable is set
It is also suitable for website-level protection, suitable for users who do not want to use program code.
Use code to judge and intercept (PHP/Python example)
In some cases, it may be inconvenient for you to modify the server configuration, and it can also be handled in the program at this time.
PHP example:
$user_agent = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/keyword/i', $user_agent)) { header('HTTP/1.1 403 Forbidden'); exit('Forbidden'); }
Python Flask example:
from flask import request, abort @app.before_request def block_user_agents(): ua = request.headers.get('User-Agent') if 'keyword' in ua: abort(403)
- This method is more flexible and can be combined with functions such as logging, dynamic blacklisting, etc.
- The disadvantage is that every request has to go through the logic, and its performance is not as good as server-level interception.
Common User-Agent types to block
Sometimes you will encounter some "suspicious" User-Agents, such as:
-
python-requests
-
Go-http-client
-
curl
-
PostmanRuntime
- Empty User-Agent
These are often not normal requests made by the browser, but may be crawlers, test tools, or even attacks. Proper blocking of such UAs can reduce garbage flow and potential risks.
Basically these are the methods. Different environments have different solutions, just choose the most convenient one. The point is to identify the User-Agent string you want to block, and then select the appropriate location to block.
The above is the detailed content of How to block specific user agents?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Sometimes, we want to block certain websites on Microsoft Edge for many reasons, whether it is for parental control, time management, content filtering, or even security concerns. A common motivation is to be more productive and stay focused. By blocking distracting websites, people can create a conducive environment for working or studying, minimizing potential distractions. Finally, content filtering is important to maintaining a safe and respectful online environment. Blocking websites that contain explicit, offensive or objectionable content is particularly important in educational or professional settings where upholding appropriate standards and values ??is crucial. If you can relate to this situation, this article is for you. Here’s how to block access to the Internet in Edge

How to prevent memory leaks in closures? Closure is one of the most powerful features in JavaScript, which enables nesting of functions and encapsulation of data. However, closures are also prone to memory leaks, especially when dealing with asynchronous and timers. This article explains how to prevent memory leaks in closures and provides specific code examples. Memory leaks usually occur when an object is no longer needed but the memory it occupies cannot be released for some reason. In a closure, when a function refers to external variables, and these variables

When we were using the win10 system, some friends encountered inexplicable blocking when downloading things. For this situation, the editor thinks it should be a problem with the system firewall. You can try to make relevant settings in the registry to solve the problem, or use the compatibility mode in the properties to open the software, etc. to solve the problem. Let’s take a look at the specific and detailed steps with the editor~ I hope it can help you. What to do if Windows 10 software is blocked from downloading. Method 1: Compatibility Mode 1. Right-click the application, click "Properties" in the menu, select the "Compatibility" tab, and check the "Compatibility Mode" option box. Method 2: Modify security settings. Find "Settings" in the lower left corner, then "Update and Security", and finally "Windows D

How to block text messages from someone on iPhone? If you receive a text message from someone you want to block, you need to open the message on your iPhone. Once you've opened the message, click on the icon at the top with the mobile number or sender's name below it. Now click on Messages on the right side of the screen; now you will see another screen with the option to block this caller. Click this button and select Block Contact. The phone number will no longer be able to send you text messages; it will also be blocked from calling your iPhone. How to unblock blocked contacts on iPhone? If you decide to allow a blocked person to send you messages, you can unblock them on your iPhone at any time. To unblock contacts on iPhone, you need to

Group Policy is a service function of Microsoft operating system. It can control the user account of the computer. Recently, some users reported that the Win7 computer prompts that this program is blocked by Group Policy. So what should I do if the Win7 computer prompts that this program is blocked by Group Policy? Solution to the problem that Win7 computer prompts that this program is blocked by group policy 1. First click Start in the lower left corner, and then select Run. 2. Then enter “gpedit.msc”. 3. Expand "User Configuration->Administrative Templates->System" on the left side. 4. Then find "Do not run specified Windows programs" in the policy on the right and double-click to open it. 5. After entering, click "Show". 6. Finally, select the program that is blocked from running in the disabled program list and remove it.

How to Block Calls Without Caller ID When you block calls from a displayed phone number, you must do this for each individual call. Luckily, Apple has built-in settings in your iPhone to block all No Caller ID calls with a few simple settings changes. To block phone calls without caller ID, go to Settings, then Phone and scroll down until you see "Mute unknown callers" (it's set to off by default), toggle it is "on". With this feature turned on, you will still receive calls when your phone number is displayed. If the phone number is blocked or held by the caller, you will not receive the call. Instead, any calls without caller ID will be sent directly to your voicemail, so

After using the win10 system for a long time, there are too many software installed on the computer, and it is inevitable that you will encounter the problem of advertising pop-ups. What should I do if it closes and reappears sometimes? How to block all pop-up ads in Win10? The editor below will teach you an effective way to remove advertising pop-ups in Windows 10. Friends who are interested, come and take a look! Method 1: Right-click "Manage" in "This PC", find "Task Scheduler Library" in "Task Scheduler" under "System Tools", and "disable" all information. Method 2: 1. Enter "internet options" in the Cortana search box and enter, click "Settings" in "Privacy". 2. Check "Show notification bar when blocking pop-ups" and change "Block Level" to "Medium".

In this article, we'll show you how to use an online calling service to block calls you don't want to receive. Call blocking effectively blocks unsolicited phone calls, especially those involving telemarketing, unwanted advertising, fraud or phishing attempts. However, sometimes an individual may choose to block someone for private reasons, such as relationship issues, disagreements, or privacy concerns. How can I call a blocked number online? If someone has blocked your number, you might consider using an online calling service to get in touch with them. Third-party services provide randomly generated numbers that you can download and make calls without the other party knowing your real number. Always exercise caution when trying to bypass blocking. This behavior may be viewed as disrespectful or even
