Hong Kong VPS servers are frequent DDoS targets — the geographic location makes them attractive for attacks originating from or passing through the Asia-Pacific region. A multi-layer defence combining Cloudflare’s global network, Nginx rate limiting, kernel-level TCP protection, and Fail2Ban blocks the vast majority of volumetric and application-layer attacks without requiring expensive dedicated DDoS mitigation services. This guide implements defence-in-depth for your Hong Kong VPS.
DDoS Defence Layers
<code">
[Internet attackers]
│
▼
Layer 1: Cloudflare (volumetric DDoS — absorbs Tbps-scale attacks)
│
▼
Layer 2: UFW + kernel sysctl (TCP SYN flood, invalid packets)
│
▼
Layer 3: Nginx rate limiting (application-layer — HTTP floods, slowloris)
│
▼
Layer 4: Fail2Ban (repeated bad actors — IP banning)
│
▼
Layer 5: Your application
Layer 1: Cloudflare in Front of Your VPS
<code"># Point your domain's DNS to Cloudflare (orange cloud = proxied) # Cloudflare absorbs volumetric DDoS at their 250+ Tbps network # Key Cloudflare settings for Asia-Pacific: # Security → DDoS → HTTP DDoS Attack Protection → Rule sensitivity: High # Security → Bot Fight Mode: ON # SSL/TLS → Always Use HTTPS: ON # Speed → Rocket Loader: ON (improves China latency) # Lock down your VPS to only accept traffic from Cloudflare IPs # (prevents attackers from bypassing Cloudflare by hitting your IP directly) # Get current Cloudflare IP ranges wget -q https://www.cloudflare.com/ips-v4 -O /tmp/cf_ips_v4.txt # UFW: allow only Cloudflare IPs on port 443, deny all others ufw deny 443/tcp # First deny all while read ip; do ufw allow from $ip to any port 443 proto tcp done < /tmp/cf_ips_v4.txt # Keep direct SSH access ufw allow 22222/tcp comment 'Admin SSH' ufw enable
Layer 2: Kernel TCP Protection (sysctl)
<code">cat >> /etc/sysctl.conf << 'EOF' # SYN flood protection net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_syn_backlog = 65536 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 5 # Drop invalid packets net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1 # Increase connection capacity net.core.somaxconn = 65535 net.core.netdev_max_backlog = 65536 # TCP TIME_WAIT and connection reuse net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 15 net.ipv4.ip_local_port_range = 1024 65535 # ICMP protection net.ipv4.icmp_echo_ignore_broadcasts = 1 net.ipv4.icmp_ignore_bogus_error_responses = 1 # IPv6 protection net.ipv6.conf.all.accept_redirects = 0 net.ipv6.conf.all.accept_source_route = 0 EOF sysctl -p
Layer 3: Nginx Rate Limiting
<code">cat > /etc/nginx/conf.d/rate_limiting.conf << 'EOF' # Rate limiting zones # General requests: 30 requests/sec per IP limit_req_zone $binary_remote_addr zone=general:10m rate=30r/s; # Login endpoints: 5 requests/min per IP limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m; # API endpoints: 100 requests/sec per IP limit_req_zone $binary_remote_addr zone=api:10m rate=100r/s; # Search: 10 requests/min per IP (expensive operations) limit_req_zone $binary_remote_addr zone=search:10m rate=10r/m; # Connection limits limit_conn_zone $binary_remote_addr zone=conn_per_ip:10m; limit_conn_zone $server_name zone=conn_per_server:10m; EOF cat > /etc/nginx/sites-available/protected-app << 'EOF'
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# Connection limits
limit_conn conn_per_ip 50; # Max 50 connections per IP
limit_conn conn_per_server 10000; # Max 10,000 total connections
# Block slowloris attacks
client_body_timeout 12s;
client_header_timeout 12s;
keepalive_timeout 15s;
send_timeout 10s;
# Block abnormally large requests
client_max_body_size 10m;
client_body_buffer_size 128k;
# Block common scanner user agents
if ($http_user_agent ~* (nikto|nmap|masscan|sqlmap|dirbuster|hydra)) {
return 444; # Drop connection silently
}
# Block empty user agents
if ($http_user_agent = "") {
return 444;
}
# General pages
location / {
limit_req zone=general burst=50 nodelay;
limit_req_status 429;
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
# Login endpoint — strict rate limiting
location ~ ^/(login|signin|api/auth) {
limit_req zone=login burst=3 nodelay;
limit_req_status 429;
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
}
# API — moderate limits
location /api/ {
limit_req zone=api burst=200 nodelay;
limit_req_status 429;
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
}
# Search
location /search {
limit_req zone=search burst=5 nodelay;
limit_req_status 429;
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
}
# Return JSON for rate limit errors
error_page 429 @ratelimit;
location @ratelimit {
default_type application/json;
return 429 '{"error":"Too Many Requests","retry_after":"1 minute"}';
}
}
EOF
nginx -t && systemctl reload nginx
Layer 4: Fail2Ban for Repeat Offenders
<code">apt install -y fail2ban cat > /etc/fail2ban/jail.local << 'EOF' [DEFAULT] bantime = 3600 # Ban for 1 hour findtime = 600 # Count failures within 10 minutes maxretry = 10 banaction = ufw # Use UFW to enforce bans [nginx-req-limit] enabled = true port = http,https filter = nginx-req-limit logpath = /var/log/nginx/error.log maxretry = 5 # Ban after 5 rate limit hits findtime = 120 # Within 2 minutes bantime = 7200 # Ban for 2 hours [nginx-http-auth] enabled = true port = http,https logpath = /var/log/nginx/error.log [nginx-botsearch] enabled = true port = http,https filter = nginx-botsearch logpath = /var/log/nginx/access.log maxretry = 3 bantime = 86400 # Ban scanners for 24 hours [sshd] enabled = true port = 22222 # Your custom SSH port maxretry = 3 bantime = 86400 EOF # Create Nginx rate limit filter cat > /etc/fail2ban/filter.d/nginx-req-limit.conf << 'EOF' [Definition] failregex = limiting requests, excess:.* by zone.*client: ignoreregex = EOF systemctl enable --now fail2ban fail2ban-client status
Monitor Attack Traffic
<code"># Real-time attack monitoring
# Top attacking IPs in last 1000 lines:
tail -1000 /var/log/nginx/access.log | \
awk '{print $1}' | sort | uniq -c | sort -rn | head -20
# Count 429 (rate limited) responses:
grep '" 429 ' /var/log/nginx/access.log | wc -l
# Banned IPs
fail2ban-client status nginx-req-limit
# Unban a legitimate IP (e.g. a partner's IP hit during load test)
fail2ban-client set nginx-req-limit unbanip LEGITIMATE_IP
# Real-time Nginx access log filter (attack traffic):
tail -f /var/log/nginx/access.log | grep -v '" 200 \|" 304 \|" 301 '
Under Attack: Emergency Response
<code"># If your VPS is under active DDoS:
# 1. Enable Cloudflare Under Attack Mode (add JS challenge to all visitors)
# Dashboard → Security → Attack Challenge → Under Attack
# 2. Temporarily rate limit more aggressively at Nginx:
# Change rate=30r/s to rate=2r/s in rate_limiting.conf
nginx -s reload
# 3. Block the top attacking subnets immediately:
# Find attacking subnet:
tail -10000 /var/log/nginx/access.log | awk '{print $1}' | \
awk -F. '{print $1"."$2"."$3".0/24"}' | sort | uniq -c | sort -rn | head -10
# Block subnet:
ufw insert 1 deny from 192.168.1.0/24 comment "DDoS block"
# 4. Enable Nginx geo-blocking (block entire countries if needed)
# Requires nginx with GeoIP2 module
Conclusion
Multi-layer DDoS protection for a Hong Kong VPS — Cloudflare absorbing volumetric attacks, kernel sysctl hardening blocking SYN floods, Nginx rate limiting stopping HTTP floods, and Fail2Ban banning persistent offenders — handles the overwhelming majority of attacks without expensive dedicated DDoS mitigation services. The HK VPS location brings its own protection advantage: Cloudflare has major Points of Presence in Hong Kong with direct CN2-connected scrubbing, keeping latency minimal for legitimate users even during active mitigation.
Protect your server: Browse Server.HK Hong Kong VPS plans — all VPS plans include the network-layer infrastructure; application-layer protection with Cloudflare and Nginx is configured by you at no additional cost.