{"id":1377,"date":"2026-09-03T11:37:59","date_gmt":"2026-09-03T06:07:59","guid":{"rendered":"https:\/\/cpanelfree.com\/blog\/how-to-protect-server-layer-7-ddos-cloudflare-ufw\/"},"modified":"2026-09-03T12:32:20","modified_gmt":"2026-09-03T07:02:20","slug":"how-to-protect-server-layer-7-ddos-cloudflare-ufw","status":"publish","type":"post","link":"https:\/\/cpanelfree.com\/blog\/how-to-protect-server-layer-7-ddos-cloudflare-ufw\/","title":{"rendered":"How to Protect Your Web Server from Layer 7 DDoS Attacks with Cloudflare &amp; UFW"},"content":{"rendered":"<div style=\"background-color: #f8fafc;border-left: 4px solid #14b8a6;padding: 20px;border-radius: 6px;margin-bottom: 25px\">\n<p style=\"margin: 0;font-size: 16px;color: #1e293b\">\n        <strong>Quick Answer:<\/strong> To protect a Linux web server from Layer 7 application-layer DDoS attacks (HTTP floods), route DNS traffic through <strong>Cloudflare Proxy (Orange Cloud)<\/strong> and lock down your server&#8217;s UFW firewall so that port 80 and 443 only accept connections from Cloudflare&#8217;s published IP ranges, preventing attackers from bypassing the CDN proxy.\n    <\/p>\n<\/div>\n<h2>Understanding Layer 7 vs Layer 4 DDoS Attacks<\/h2>\n<p>While Layer 4 volumetric attacks (SYN floods, UDP amplification) attempt to saturate network bandwidth, <strong>Layer 7 Application Attacks<\/strong> mimic legitimate user requests (such as heavy search queries or database POST requests). These attacks consume 100% of PHP-FPM workers and MySQL CPU buffers, crashing the web server with minimal attacker bandwidth.<\/p>\n<h2>Step 1: Enabling Cloudflare Proxy and WAF Protection<\/h2>\n<ol style=\"padding-left: 20px;line-height: 1.8\">\n<li>In your Cloudflare dashboard, ensure all root and sub-records have the <strong>Proxy status set to Proxied (Orange Cloud)<\/strong>.<\/li>\n<li>Navigate to <strong>Security &gt; WAF &gt; Rate Limiting Rules<\/strong> and create a rule restricting any single IP to a maximum of 50 requests per 10 seconds.<\/li>\n<li>During an active attack, toggle <strong>Under Attack Mode<\/strong> to enforce a managed JavaScript\/Turnstile verification challenge.<\/li>\n<\/ol>\n<h2>Step 2: Locking Down Origin IP in UFW (Prevent Direct Bypass)<\/h2>\n<p>If an attacker discovers your direct origin server IP address, they can send traffic directly to your VPS, bypassing Cloudflare completely. To eliminate this vector, configure UFW to allow port 80\/443 traffic <em>only<\/em> from Cloudflare reverse proxy IPs:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">#!\/bin\/bash\n# Sync Cloudflare IP ranges with UFW\nfor ip in $(curl -s https:\/\/www.cloudflare.com\/ips-v4); do\n    sudo ufw allow from $ip to any port 80,443 proto tcp\ndone\n\n# Deny all other direct incoming web connections\nsudo ufw deny 80\/tcp\nsudo ufw deny 443\/tcp\nsudo ufw reload<\/pre>\n<h2>Step 3: Restoring Real Visitor IPs in Nginx \/ Apache<\/h2>\n<p>Because all incoming traffic now arrives from Cloudflare proxy IPs, configure your web server to read the <code>CF-Connecting-IP<\/code> HTTP header so access logs and Fail2ban see true client IPs:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># \/etc\/nginx\/conf.d\/cloudflare.conf\nset_real_ip_from 173.245.48.0\/20;\nset_real_ip_from 103.21.244.0\/22;\nset_real_ip_from 104.16.0.0\/13;\nreal_ip_header CF-Connecting-IP;<\/pre>\n<h2>Local Web Server Rate Limiting with Nginx &amp; OpenLiteSpeed<\/h2>\n<p>Even with Cloudflare proxy active, configure local web server rate limiting to prevent memory exhaustion if attackers target un-cached search queries or API endpoints:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># \/etc\/nginx\/nginx.conf\n# Define shared memory zone for rate limiting (10 MB stores 160,000 IPs)\nlimit_req_zone $binary_remote_addr zone=one:10m rate=10r\/s;\nlimit_conn_zone $binary_remote_addr zone=addr:10m;\n\n# Apply rate limiting to virtual host\nlocation \/ {\n    limit_req zone=one burst=20 nodelay;\n    limit_conn addr 10;\n    try_files $uri $uri\/ \/index.php?$args;\n}<\/pre>\n<h2>Automated Origin IP Blackhole via Fail2ban Cloudflare API<\/h2>\n<p>You can configure Fail2ban to communicate directly with Cloudflare&#8217;s REST API. When Fail2ban detects malicious Layer 7 floods locally, it dispatches an API request to Cloudflare to block the offending IP globally across all Cloudflare edge data centers.<\/p>\n<h2>Layer 7 DDoS Mitigation: Nginx FastCGI Microcaching vs Dynamic Floods<\/h2>\n<p>When Layer 7 attackers flood dynamic WordPress PHP endpoints (like <code>\/?s=random_query<\/code>), PHP-FPM processes and MySQL CPU usage immediately spike to 100%. Implementing <strong>Nginx FastCGI Microcaching<\/strong> caches dynamic HTML pages for 1 to 5 seconds, allowing your server to absorb tens of thousands of requests per second directly from memory without invoking PHP or MySQL:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># \/etc\/nginx\/conf.d\/microcache.conf\nfastcgi_cache_path \/var\/cache\/nginx levels=1:2 keys_zone=MICROCACHE:10m max_size=500m inactive=60m;\nfastcgi_cache_key \"$scheme$request_method$host$request_uri\";\n\nserver {\n    set $skip_cache 0;\n    if ($request_method = POST) { set $skip_cache 1; }\n    if ($query_string != \"\") { set $skip_cache 0; } # Cache search floods!\n\n    location ~ \\.php$ {\n        fastcgi_cache MICROCACHE;\n        fastcgi_cache_valid 200 301 302 2s; # Cache for 2 seconds\n        fastcgi_cache_use_stale error timeout updating invalid_header http_500;\n        fastcgi_no_cache $skip_cache;\n        fastcgi_cache_bypass $skip_cache;\n        include snippets\/fastcgi-php.conf;\n        fastcgi_pass unix:\/var\/run\/php\/php8.3-fpm.sock;\n    }\n}<\/pre>\n<h2>Testing DDoS Resilience with Simulated Load Tools<\/h2>\n<p>Benchmark your server&#8217;s rate limiting and caching resilience using HTTP load-testing utilities like <code>wrk<\/code> or <code>vegeta<\/code> from an external test instance to verify that un-cached floods are successfully dropped before consuming server memory.<\/p>\n<h2>Mitigating Slowloris &amp; Slow HTTP Post Attacks on Nginx<\/h2>\n<p><strong>Slowloris<\/strong> attacks keep thousands of HTTP connections open simultaneously by sending headers extremely slowly, exhausting the web server&#8217;s connection pool without triggering standard volumetric packet thresholds. Harden Nginx timeout directives to drop slow connection attempts aggressively:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># \/etc\/nginx\/conf.d\/anti_slowloris.conf\n# Aggressive timeouts to terminate slow-drip attacker sockets\nclient_body_timeout 10s;\nclient_header_timeout 10s;\nkeepalive_timeout 15s;\nsend_timeout 10s;\nreset_timedout_connection on;<\/pre>\n<h2>Automated Origin IP Blackhole Synchronization via Cron<\/h2>\n<p>Cloudflare periodically updates its global reverse proxy IP ranges. Create an automated monthly cron script to ensure your UFW firewall rules remain synchronized with Cloudflare&#8217;s published CIDR blocks:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># \/etc\/cron.monthly\/update-cloudflare-ufw.sh\n#!\/bin\/bash\ncurl -s https:\/\/www.cloudflare.com\/ips-v4 -o \/tmp\/cf_ips.txt\nfor ip in $(cat \/tmp\/cf_ips.txt); do\n    sudo ufw allow from $ip to any port 80,443 proto tcp\ndone\nsudo ufw reload<\/pre>\n<div style=\"background-color: #f8fafc;border: 1px solid #e2e8f0;border-left: 4px solid #0ea5e9;padding: 20px;border-radius: 8px;margin: 30px 0\">\n<h3 style=\"margin-top: 0;color: #0f172a;font-size: 18px;display: flex;align-items: center\">\n        <span style=\"margin-right: 8px\">\ud83d\udd17<\/span> Recommended Related Technical Guides:<br \/>\n    <\/h3>\n<ul style=\"margin: 10px 0 0 0;padding-left: 20px;line-height: 1.8\">\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-setup-free-cloudflare-cdn-dns-hosting\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Set Up Free Cloudflare CDN &amp; DNS on Any Web Hosting<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/dns-a-record-vs-cname-vs-alias-guide\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">DNS A Record vs CNAME vs ALIAS Explained<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-fix-dns-probe-finished-nxdomain-error\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Fix DNS_PROBE_FINISHED_NXDOMAIN Error<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/best-free-web-hosting-2026\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Top 10 Best Free Web Hosting Services<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/#plans\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Explore $0 Free cPanel Web Hosting Plans<\/a><\/li>\n<\/ul>\n<\/div>\n<div style=\"background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);border: 1px solid #334155;border-radius: 12px;padding: 25px;margin: 30px 0;text-align: center\">\n<h3 style=\"color: #38bdf8;margin-top: 0;font-size: 20px\">Built-in Anti-DDoS Protection on CpanelFree<\/h3>\n<p style=\"color: #94a3b8;font-size: 14px;line-height: 1.6;max-width: 600px;margin: 0 auto 15px\">\n        Want instant DDoS resilience without complex iptables scripts? <strong>CpanelFree<\/strong> provides multi-terabit edge mitigation, real-time WAF filtering, and free cPanel hosting at $0 forever.\n    <\/p>\n<p>    <a href=\"https:\/\/cpanelfree.com\/free-wordpress-hosting\" style=\"display: inline-block;background-color: #14b8a6;color: #ffffff;padding: 10px 22px;border-radius: 6px;text-decoration: none;font-weight: bold;font-size: 14px\">Launch Free Website<\/a>\n<\/div>\n<h2>Frequently Asked Questions<\/h2>\n<div style=\"border-bottom: 1px solid #e2e8f0;padding: 12px 0\">\n<h4 style=\"margin: 0 0 8px 0;color: #1e293b\">How can an attacker find my hidden origin server IP?<\/h4>\n<p style=\"margin: 0;color: #475569;font-size: 14px\">Common origin leaks include historical DNS records (SecurityTrails), outbound email headers (SMTP sending from origin IP), or non-proxied subdomains (e.g. <code>mail.yourdomain.com<\/code> or <code>cpanel.yourdomain.com<\/code>).<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Quick Answer: To protect a Linux web server from Layer 7 application-layer DDoS attacks (HTTP floods), route DNS traffic through Cloudflare Proxy (Orange Cloud) and lock down your server&#8217;s UFW firewall so that port 80 and 443 only accept connections from Cloudflare&#8217;s published IP ranges, preventing attackers from bypassing the CDN proxy. Understanding Layer 7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1376,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-1377","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-security"],"_links":{"self":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/comments?post=1377"}],"version-history":[{"count":4,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1377\/revisions"}],"predecessor-version":[{"id":1559,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1377\/revisions\/1559"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media\/1376"}],"wp:attachment":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media?parent=1377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/categories?post=1377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/tags?post=1377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}