What is it and how we block the access of a User Agent (Bot or Browser) on a website [NGINX Tips]

First of all, let's see in broad terms what this user-agent of an Internet browser is and what he uses.

What is User Agent (or User-Agent)?

Everything that means an Internet browser (Internet Explorer, Safari, Opera, Firefoc, Chrome, etc.) has a software agent that interacts with the website you are visiting, at the host server level. This software makes the internet connection with the website, understands, verifies and will render the content of the page accessed. In short, it ensures the interaction between the user and the host server that serves the user the requested web pages by accessing a specific URL on the HTTP / HTTPS protocol.
User Agent contains in the header delivered to the server it accesses, information about the software used by the user, the operating system, the version of the software package of the browser. For example, the Google Chrome user-agent will provide the server with information about the version of Google Chrome used and the user's operating system.

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3163.100 Safari/537.36

The header user-agent above means that a version of Google Chrome 70 on a computer with AMD processor, 64-bit architecture OS Windows 10.
You will probably wonder why Mozilla is appearing as a Chrome user-agent. This is a kind of trick used by Google (and Microsoft in IE) to facilitate access by Chrome users to all Mozilla and Safari compatible sites. AppleWebKit is the content playback engine.

Webmasters or web server administrators can block the access of certain user agents on webhosts hosted. Most of the time, this limitation is set web robots (web bots) that chaotic or excessive access to the web pages of a website. Fortunately, these bots have a specific user-agent and can be easily detected in web server logs.

How to block user-agent access on a web server with NGINX

Let's take the scenario where we no longer want to allow access to the website for users with Windows XP operating systems and Mozilla Firefox 5.0 Internet browser.

We add the code below the file nginx configuration, to the "server" block:

if ($http_user_agent = "Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Firefox/52.0"){
return 403;
}

Save the file nginx.conf or the file specific to a website, if you use custom configuration (gen sites-available), then restart the NGINX service.
All users with Firefox 5.0 (including all versions up to 5.9) on Windows XP will be redirected to error 403 (Forbidden).

I didn't accidentally choose this example. Starting with Firefox 52.9.0, Mozilla has stopped support for Windows XP and Windows Vista users. Operating systems whose support was also stopped by Microsoft in 2014, respectively 2017 for Windows Vista.

What is it and how we block the access of a User Agent (Bot or Browser) on a website [NGINX Tips]

About the author

Stealth

Passionate about everything that means gadgets and IT, I write with pleasure stealthsettings.com from 2006 and I like to discover with you new things about computers and operating systems macOS, Linux, Windows, iOS and Android.

Leave a Comment