You're building a SaaS application and need to set up a solid infrastructure on DigitalOcean. The setup involves multiple Droplets, a managed database, load balancing, and most importantly, protection against DDoS attacks. Let's walk through how to configure this properly and where Cloudflare fits into the picture.
The architecture you're considering is actually quite sensible. You'll have two application servers running your Laravel app, a third Droplet handling load balancing duties, a managed database that both servers connect to, and DigitalOcean Spaces for static assets. This kind of multi-server setup provides redundancy and helps distribute traffic effectively.
The key question is where to place your DDoS protection layer. You're right that Cloudflare should sit at the very front of your infrastructure. When someone visits your site, their request hits Cloudflare first, which filters out malicious traffic before anything reaches your servers. From there, clean traffic passes to your load balancer, which distributes requests between your two application servers.
Cloudflare works by becoming your public-facing DNS. When you configure it correctly, all traffic routes through their network before reaching your actual servers. This means DDoS attacks get absorbed by Cloudflare's infrastructure rather than overwhelming your Droplets.
For your configuration, you'd point your domain's DNS to Cloudflare, then configure Cloudflare to forward legitimate traffic to your load balancer's IP address. The load balancer then does its job of distributing requests between your two application servers. Both servers connect to the managed database on the backend, which remains protected because it's not directly exposed to the internet.
One thing worth noting: your managed database should only accept connections from your application servers' IP addresses. DigitalOcean lets you configure firewall rules for managed databases, so you can whitelist only your Droplets. This adds an extra security layer that keeps your database safe even if someone discovers its endpoint.
DigitalOcean Spaces comes with its own CDN, but you're right to think about additional protection. The good news is you can put Cloudflare in front of Spaces too. Instead of pointing directly to your Spaces URL, you'd create a CNAME record that routes through Cloudflare first. This way, requests for images, videos, or other assets also benefit from Cloudflare's DDoS protection and caching.
The setup involves configuring a custom domain for your Space, then adding that domain to Cloudflare. Traffic hits Cloudflare, gets cached and filtered, then reaches Spaces only when necessary. This approach also speeds up content delivery since Cloudflare's edge network is quite extensive.
Here's where many setups fail: even with Cloudflare enabled, attackers can sometimes discover your origin server's real IP address and attack it directly, bypassing Cloudflare entirely. This happens more often than you'd think.
The most effective protection is configuring your firewall to only accept traffic from Cloudflare's IP ranges. DigitalOcean's Cloud Firewall feature lets you create rules that whitelist only Cloudflare's published IP addresses. Any traffic trying to hit your servers directly gets blocked at the firewall level.
You should also avoid situations where your real IP might leak. Don't send emails directly from your Droplets without routing through a service that masks your IP. Be careful with DNS records—make sure any subdomains also proxy through Cloudflare rather than pointing directly to your servers. Check for old DNS records that might still expose your original IPs.
When setting up new Droplets specifically for this protected infrastructure, consider getting fresh IPs that have never been publicly associated with your domain. If you're migrating from an existing setup, the old IPs might already be known and recorded in various databases.
Your proposed architecture is solid for a growing SaaS application. The dual-server setup with load balancing provides redundancy, so if one server has issues, traffic automatically flows to the other. The managed database takes care of backups and maintenance, and Spaces with CDN handles static assets efficiently.
👉 Explore high-performance server solutions with enterprise-grade DDoS protection built in
However, there's one consideration: using a Droplet as a load balancer works, but DigitalOcean also offers a managed Load Balancer service. The managed option costs more but removes a single point of failure. If your load balancer Droplet goes down with your current plan, both application servers become unreachable even though they're still running fine. The managed Load Balancer has built-in redundancy.
For a production SaaS application handling real users and revenue, the managed Load Balancer is usually worth the extra cost. It also integrates better with DigitalOcean's monitoring and automatically handles SSL certificate management.
Beyond DDoS protection, think about rate limiting at the Cloudflare level. You can set rules that limit how many requests a single IP can make within a timeframe, which helps prevent both DDoS attacks and application-level abuse.
Enable Cloudflare's WAF (Web Application Firewall) rules to protect against common web exploits. Laravel applications are generally secure, but an extra layer never hurts, especially against zero-day vulnerabilities.
For your database connections, use private networking if DigitalOcean offers it in your region. This keeps database traffic off the public internet entirely, adding another security layer. Your application servers communicate with the database over DigitalOcean's internal network, which is both faster and more secure.
Start by spinning up your Droplets and managed database. Configure the database firewall to only accept connections from your application servers. Set up your application on both servers—Laravel Forge makes this relatively straightforward.
Once your application is working, configure the load balancer (whether a Droplet running nginx or the managed service). Test that it properly distributes traffic between both servers and handles failover when one goes offline.
Next, add your domain to Cloudflare and configure the DNS to point to your load balancer. Enable Cloudflare's proxy feature and turn on DDoS protection. Configure your firewall rules to whitelist only Cloudflare IPs.
Finally, set up Spaces with a custom domain proxied through Cloudflare, and configure your Laravel application to serve static assets from there.
The whole process takes time to get right, but once configured properly, you'll have a robust infrastructure that can handle growth and defend against attacks. Test everything thoroughly before going live, and monitor your logs to ensure Cloudflare is catching threats as expected.