Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 30 ยท Security and Hardening

Access Control and Rate Limiting

Restrict who can reach a URL with allow/deny, Require ip and Basic auth, then slow abusive clients with limit_req, limit_conn and fail2ban.

A web server that answers every request from every address is a good start and a poor finish. Some URLs (an admin panel, a status page, a metrics endpoint) should only be reachable from your office or after a login. Other URLs (a login form, a search box, an expensive API call) can be reached by anyone, but nobody should be able to hammer them a thousand times a second.

Those are two different problems with two different tool sets. Access control decides who may make a request at all. Rate limiting decides how fast an allowed client may keep making requests. In this lesson you will use both on nginx and Apache, see what each server can and cannot do on its own, and play with a leaky-bucket simulator that behaves like nginx's limit_req.

Real nginx and Apache configuration appears in read-only blocks, because the site cannot run a web server. The two interactive examples are small JavaScript stand-ins that reproduce the decision logic so you can experiment. They are not nginx or Apache.

Three layers of protection

Network rules Allow or deny by IP address or range.
Authentication Ask for a user name and password.
Rate limits Cap requests and connections per client.

Combine layers. Each one catches what the others miss.

1 / 2

Allowing and denying by IP address

On nginx, allow and deny come from the access module. Rules are checked in order, and the first rule that matches decides. If no rule matches, the request is allowed, which is why a block of allow lines almost always ends with deny all;.

NGINX
location /admin/ {
    allow 192.168.1.0/24;   # the office network
    allow 203.0.113.7;      # one admin working from home
    deny  all;              # everybody else gets 403 Forbidden
}

Order is the classic trap. Put deny all; first and every later allow is unreachable. The stand-in below applies the same first-match rule to three rule lists so you can see the difference.

JS Stand-in: first matching rule wins

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the last column: without a closing deny all;, a stranger at 198.51.100.9 walks straight in. That is the most common access-control bug on real servers.

Apache 2.4 uses the Require directive from mod_authz_core instead. It reads like a sentence, and several lines are combined with an implicit OR unless you wrap them in <RequireAll>:

APACHE
<Location "/admin">
    Require ip 192.168.1.0/24 203.0.113.7
</Location>

Because Require already means "only these", there is no deny all to forget: anything not listed is refused. You may still see Order allow,deny and Allow from in old guides. Those belong to Apache 2.2 and only work in 2.4 through a compatibility module, so do not write new configuration with them.

Rules by country or network list

To block a list of addresses, or to reuse one list in several places, nginx's geo directive maps an address to a variable:

NGINX
geo $blocked {
    default          0;
    198.51.100.0/24  1;
    192.0.2.0/24     1;
}

server {
    if ($blocked) { return 403; }
}

An if that only contains return is one of the few safe uses of if in nginx. For country-level rules you need a GeoIP database and a module that reads it, which is not part of a default build, so check what your distribution packages. Keep in mind that IP-based country blocking is easy to evade and sometimes wrong, so treat it as a blunt tool.

Behind a CDN, load balancer or another proxy, the address nginx sees is the proxy's, not the visitor's. Restore the real client address first (nginx real_ip module with set_real_ip_from, Apache mod_remoteip; see Reverse Proxy Basics). Otherwise every rule in this lesson applies to the proxy, and one visitor's behavior can lock everybody out.

Password protection with Basic authentication

HTTP Basic authentication makes the browser show a login box and send the credentials on every request. It is not pretty and it is not a full account system, but for a staging site or an internal tool it takes two minutes. Only use it over HTTPS, because the password is merely Base64-encoded (see HTTPS, TLS and Let's Encrypt).

First create a password file. The htpasswd tool comes from apache2-utils on Debian or httpd-tools on RHEL, and it works for both servers:

Terminal
sudo htpasswd -c /etc/nginx/.htpasswd alice    # -c creates the file; leave it out for later users
sudo htpasswd /etc/nginx/.htpasswd bob

nginx:

NGINX
location /staging/ {
    auth_basic           "Staging only";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

Apache:

APACHE
<Location "/staging">
    AuthType Basic
    AuthName "Staging only"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Location>

Keep the password file outside the document root, readable by the server user and nobody else.

A common wish is "no password from the office, a password from anywhere else". Both servers can do that:

NGINX
location /staging/ {
    satisfy any;                       # IP rule OR password is enough
    allow 192.168.1.0/24;
    deny  all;
    auth_basic           "Staging only";
    auth_basic_user_file /etc/nginx/.htpasswd;
}
APACHE
<Location "/staging">
    AuthType Basic
    AuthName "Staging only"
    AuthUserFile /etc/apache2/.htpasswd
    <RequireAny>
        Require ip 192.168.1.0/24
        Require valid-user
    </RequireAny>
</Location>

In nginx the default is satisfy all, which needs both. Writing satisfy any is the explicit switch to "either one".

Rate limiting with a leaky bucket

Imagine a bucket with a small hole in the bottom. Requests pour in at the top, and the hole lets them out at a fixed speed. A short splash fits in the bucket and drains away. A sustained flood overflows, and the overflow is rejected. That picture is how nginx's limit_req works, and the numbers you configure map directly onto it:

  • rate: how fast the hole drains, for example 5r/s. nginx tracks it per millisecond, so 2r/s means one request per 500 ms, not "two any time in the second".
  • burst: how many extra requests the bucket can hold above the steady rate.
  • nodelay: whether queued requests are served at once or held back to match the rate.
NGINX
http {
    # one counter per client IP, 10 MB of shared memory (roughly 160,000 addresses)
    limit_req_zone $binary_remote_addr zone=perip:10m rate=5r/s;
    limit_req_status 429;

    server {
        location /login {
            limit_req zone=perip burst=10 nodelay;
        }
    }
}

The zone is declared once in http and used in a server or location. The key ($binary_remote_addr) decides who shares a counter; use another variable such as an API key header to limit per account. Without limit_req_status, rejected requests get 503, which looks like an outage in your monitoring, so 429 Too Many Requests is clearer.

Run the simulator below. Ten requests arrive in the same instant, and then a steady trickle shows that exact spacing matters.

JS Stand-in: a leaky bucket like limit_req

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice three things. With no burst, a rate of 2 per second serves one request of a simultaneous ten. With burst=4 the bucket holds four more, so five are served, but each queued one is held back (500 ms, 1000 ms, ...) so the backend still sees only two per second. With nodelay the same five are served immediately, and the bucket then needs time to drain before new bursts fit. Most sites want burst plus nodelay: forgiving to a page that fires several requests at once, strict about sustained floods. The last scenario shows how exact the rate is: at 2r/s, a request every 400 ms is too fast, so every second one is rejected, while a request every 500 ms would pass. Real nginx also has delay=N to serve the first N queued requests at once and delay the rest.

Limiting simultaneous connections

limit_req counts requests over time. limit_conn counts connections open right now, which suits downloads and slow clients that hold sockets:

NGINX
limit_conn_zone $binary_remote_addr zone=conn_ip:10m;
limit_conn_status 429;

server {
    location /downloads/ {
        limit_conn conn_ip 4;      # at most four at once per IP
        limit_rate 500k;           # and 500 KB/s per connection
    }
}

Apache has no built-in per-request-rate limiter. The bundled mod_ratelimit is a bandwidth limiter, so it plays the role of nginx's limit_rate, not limit_req:

APACHE
<Location "/downloads">
    SetOutputFilter RATE_LIMIT
    SetEnv rate-limit 400          # KiB per second per response
</Location>

For request rates and per-client connection caps on Apache you need a third-party module (mod_evasive, mod_qos and similar) or you let nginx, a CDN or a firewall do it in front. That is one of several reasons people put nginx in front of Apache (see Nginx in Front of Apache).

Apache also has mod_reqtimeout, enabled by default on many systems, which closes connections that send their request too slowly. It is the first defense against "slowloris"-style attacks, and the matching nginx settings (client_header_timeout, client_body_timeout) appear in Hardening and Security Headers.

Banning repeat offenders with fail2ban

Rate limits slow a client down. They do not remember that it has been rude all day. fail2ban is a separate program that watches log files, counts matching lines per IP, and adds a firewall rule to ban an address that misbehaves too often. It ships filters for typical web cases such as failed Basic-auth logins and clients that keep tripping limit_req (look for the nginx-http-auth, nginx-limit-req and apache-auth filters on your system). A jail is a few lines:

INI
[nginx-http-auth]
enabled  = true
maxretry = 5
findtime = 10m
bantime  = 1h

The idea is simple: the web server writes the evidence into its error log (see Logs and Troubleshooting), and fail2ban turns evidence into a firewall ban. Test with your own address on a spare connection first, so you do not lock yourself out.

Practical advice

  • Rate-limit the expensive paths (login, search, password reset, API writes), not the whole site. A page full of images easily makes 30 requests.
  • Do not rate-limit health checks from your load balancer. Give them a location with no limit_req.
  • Shared addresses (offices, mobile carriers, schools) hide many users behind one IP, so keep bursts generous.
  • Log rejections at a level you can see, and watch for them after every deploy.
  • Access control is not a substitute for application security: a good password and an up-to-date app matter more.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. An nginx location contains deny all; followed by allow 10.0.0.0/8;. What happens to a request from 10.1.2.3?

    allow and deny are evaluated in order and the first match wins. deny all matches everyone, so the later allow is never reached.
  2. 2. What does limit_req_zone $binary_remote_addr zone=perip:10m rate=5r/s; do on its own?

    The zone is a declaration. You must add limit_req zone=perip ... in a server or location to apply it.
  3. 3. What does adding nodelay to limit_req ... burst=10 change?

    Without nodelay, queued burst requests are delayed so the backend sees the steady rate. With nodelay they are served at once, and the slots refill over time.
  4. 4. Which Apache tool limits the number of requests a client may make per second out of the box?

    mod_ratelimit throttles the speed of a response, like nginx's limit_rate. Per-request limits need extra modules, nginx, a CDN or a firewall.
  5. 5. Why must you restore the real client IP before using IP-based rules behind a proxy?

    Without real_ip (nginx) or mod_remoteip (Apache) the server sees the proxy as the client, so one noisy visitor can get everyone limited or blocked.

Summary

  • allow/deny in nginx are checked in order and the first match wins; end allow lists with deny all;. Apache 2.4 uses Require ip, where anything not listed is refused.
  • Basic authentication (auth_basic or AuthType Basic with an htpasswd file) is quick protection for internal pages, but only over HTTPS. satisfy any or <RequireAny> combines IP and password rules.
  • limit_req is a leaky bucket: rate is the drain speed, burst the extra capacity, nodelay serves queued requests at once. Use limit_req_status 429 for clearer errors.
  • limit_conn caps simultaneous connections; Apache's mod_ratelimit limits bandwidth, not request rate.
  • fail2ban reads the logs and bans repeat offenders at the firewall.
  • Always restore the real client IP behind proxies, and rate-limit expensive paths rather than the whole site.

Next: Hardening and Security Headers.