Bobcares

Nginx brute force protection – 2 proven ways to mitigate this attack

by | Jan 9, 2019

Bruteforce attacks have become a nightmare for all website owners.

And to secure their websites, they set strong username and password. Is it sufficient?

Well, this keeps your account safe. But, there’s another side as well. Bruteforce attacks consume many server resources and bandwidth that slow down your websites.

Fortunately, server owners can use Nginx as a reverse proxy and rate limit traffic to mitigate bruteforce attacks.

As part of our Server Management Services, we help server owners set up Nginx as reverse proxy and enable bruteforce protection to secure their servers.

Today, let’s discuss how to enable Nginx brute protection.

 

Nginx brute force protection – 2 proven methods

Now let’s see how our Server Administration Team enables Nginx brute force protection.

 

1) Enable Rate Limit in Nginx

Rate limiting is one of the best ways to slow down bruteforce attacks.

It allows you to limit the number of HTTP requests(GET and POST) that can be made by the user within a certain period. Moreover, it can deny requests beyond a specific threshold.

Our Server Experts always enable Nginx rate limit feature during server hardening.

Most importantly, we tweak the limit_req_zone parameter in the Nginx configuration file “/etc/nginx/nginx.conf“, to rate limit the requests.

For instance, see the below code.

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

 

This adds a zone called one, that can hold up to 160,000(as 1m =16,000 IPs) unique IP addresses, and the 1r/s parameter means that only one request is allowed per second.

Similarly, we can rate limit the connections to a particular file with limit_req parameter.

For example, we add the following code in nginx to protect WordPress from bruteforce attacks.

location = /wp-login.php {
limit_req  zone=one burst=5 nodelay;
}

 

This allows 5 requests in 5 seconds. And, if this exceeds, Nginx returns a 503 Service Temporarily Available error until the request rate is reduced.

Moreover, our Support Experts use limit_conn_zone and limit_conn directives to limit the number of connections from each IP address.

 

[And, do you need help to enable Rate Limit on your Nginx reverse proxy? One of our Server Experts can help you here.]

 

2) Install Fail2ban

Fail2ban is a great tool for server owners to automatically ban suspicious IPs in the server firewall.

It scans Nginx log files for failed login attempts, and ban these IPs for a specific period of time using firewall.

So to enable it, our Hosting Engineers analyze the nginx log files first and obtain a specific pattern for non-existent users.

After that, we create fail2ban filter files in the location /etc/fail2ban/filter.d/. This specifies the patterns to look within the nginx logs.

For instance, we configure a filter file say nginx-auth.conf, and add the following rules.

failregex = no user/password was provided for basic authentication.*client: <HOST>
user .* was not found in.*client: <HOST>
user .* password mismatch.*client: <HOST>
ignoreregex = </host></host></host>

 

This matches the lines where user has not entered the username and password.

Finally, we configure a fail2ban jail configuration in the location /etc/fail2ban/jail.local. This defines the the nginx log file path, filter name, bantime, etc.

[nginx-auth]
enabled = true
filter = nginx-auth
port = http,https
logpath = /var/log/nginx*/*error*.log
findtime =60
bantime = 500
maxretry = 3

 

This scans the nginx log files and attempts to match the regex pattern in the filter. If a match is found, it blocks that IP address in the server firewall.

[And, do you need a Server Expert to setup Fail2ban on your server? Click here, and one of our Server Experts will asses your requirements and assist you.]

 

Conclusion

In short, bruteforce attacks have become common these days. Today, we’ve discussed the top 2 methods to enable Nginx brute force protection and how our Support Engineers implement it.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

SEE SERVER ADMIN PLANS

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

2 Comments

  1. Diego

    How can fail2ban block IPswhen nginx is above a CDN?

    Reply
    • Sijin George

      Hello Diego,
      Fail2ban can block user IP address when you enable the ‘set_real_ip_from’ parameter in your configuration. If you need further help, we’ll be happy to talk to you on chat (click on the icon at right-bottom).

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF