No KYC VPS
SecurityNov 14, 20268 min readBy the No KYC VPS team

Layer 7 DDoS Defence for Your VPS: Nginx, Fail2ban and Caching

Network filtering stops floods, but HTTP floods hit your application. Learn rate limiting in nginx, fail2ban rules, caching and when to put a CDN in front.

DDoS attacks come in two broad kinds. Layer 3/4 attacks flood the network with packets, and the datacenter filters them upstream. Layer 7 attacks send many ordinary-looking web requests that make your application work hard. The network cannot always tell those from real visitors, so you add defences on the server. See what our network filters in DDoS-protected VPS.

1. Rate limit in nginx

Limit how fast one IP can request pages, and keep a tighter limit on expensive URLs such as login and search:

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

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

2. Limit connections

limit_conn_zone $binary_remote_addr zone=conn:10m;
limit_conn conn 20;
client_body_timeout 10s;
client_header_timeout 10s;
send_timeout 10s;

Short timeouts stop slow-connection attacks from tying up workers.

3. Ban repeat offenders with fail2ban

Fail2ban reads your logs and adds firewall rules for abusive addresses. Enable the nginx rules and tune the ban time. Our guide on securing a new VPS covers the install.

4. Cache what you can

A page served from cache costs almost nothing. Use nginx fastcgi_cache or proxy_cache for public pages, and a static-file cache for assets. A site that is mostly cached barely notices a request flood.

5. Make expensive endpoints cheap

  • Add indexes to the database queries behind search and listing pages.
  • Put heavy actions behind a login or a captcha.
  • Return an error early for obviously bad requests, such as unusual methods.

6. Consider a CDN

A CDN absorbs traffic away from your origin, but it also sees your visitors' traffic. Whether that suits you depends on your privacy needs. If you use one, keep your origin address private. See CDN for your VPS.

Rate-limit requests at the web server

A layer 7 attack looks like normal web traffic, so provider filtering at the network level often does not stop it. Start with a limit in nginx:

limit_req_zone $binary_remote_addr zone=perip:10m rate=10r/s;
server {
    location / {
        limit_req zone=perip burst=20 nodelay;
    }
}

Tune the rate to your real traffic. Apply stricter limits to login pages and search, which cost the most to serve.

Make each request cheaper and the target smaller

  • Cache. Serve static pages from cache so a flood of requests does not reach your application or database.
  • Hide the origin. Put a CDN in front and accept web traffic only from its addresses, so attackers cannot hit the server directly.
  • Ban repeat offenders. Use fail2ban or a similar tool to block addresses that break your limits again and again.

7. Watch and prepare

Check access.log during a spike to see which URL and which addresses are involved, then target your rule at that. Keep backups in case the worst happens.

Frequently asked questions

What is a layer 7 DDoS attack?

A flood of ordinary-looking HTTP requests that overloads your application rather than the network.

Can the datacenter stop layer 7 attacks?

Network filtering blocks floods of packets. HTTP floods look like real visitors, so rate limiting and caching on the server matter.

Does caching help against DDoS?

Yes. A cached page costs almost nothing to serve, so a request flood has little effect.

Do I need a CDN?

Not always. It can absorb traffic, but it also sees your visitors' traffic, so consider your privacy needs.

Is layer 7 protection included with a VPS?

Network-level protection usually covers large floods, not application-level requests. Layer 7 needs measures on your own server or a service in front of it.

Will rate limiting block real visitors?

It can if the limit is too low. Start generous, watch your logs, and tighten step by step.