Your VPS runs the site, but it does not have to face the internet alone. Putting cloudflare in front of vps traffic gives you a shield between visitors and your server: Cloudflare answers requests first, filters out junk, and only passes clean traffic to your origin. The setup takes under an hour, costs nothing on the free plan for most small sites, and it changes how your server handles attacks, bad bots, and sudden traffic spikes.
Cloudflare sits in three roles at once. It is your DNS host, so it answers the question of where your domain points. It is a reverse proxy, so visitor traffic flows through Cloudflare servers before it reaches your VPS. And it is a web application firewall, so it can block requests that look like attacks before they touch your code. Each role is simple on its own, and the order you set them up in matters, because a wrong DNS record or an open origin port can undo the protection entirely. This cloudflare dns proxy setup walks through all three roles in order, so nothing gets skipped.
How to put Cloudflare in front of VPS traffic
Start with an account and add your site. Cloudflare scans your current DNS records and imports them, then asks you to change your nameservers at your registrar to the two Cloudflare nameservers it assigns. This step hands DNS control to Cloudflare. Before you change anything, write down every existing DNS record from your registrar or current DNS host, including mail records, TXT verification records, and subdomains, so nothing gets lost in the move. If you need a refresher on what each record type does, read our guide to DNS records, from A and AAAA to CNAME, MX, and TXT before you touch the zone.
Once the nameservers point to Cloudflare, confirm the zone is active in the dashboard, then check that your site still loads. DNS changes take time to spread, and the TTL on each record controls how long old answers stay cached. Our explainer on how DNS TTL caching really works shows why a record can look wrong for hours after you fix it. Set the TTL low, around 300 seconds, while you are making changes, then raise it later when the setup is stable.
Point DNS at your VPS and turn on the proxy
Create an A record for your root domain that points to your VPS IPv4 address, and an AAAA record if your VPS has IPv6. Add a CNAME for www that points to your root domain. Then look at the cloud icon next to each record. An orange cloud means the record is proxied, and that orange cloud is what makes the cloudflare orange cloud proxy work: visitors connect to Cloudflare, and Cloudflare connects to your VPS. A grey cloud means DNS only: visitors connect straight to your VPS and skip every Cloudflare feature.
Proxy the records that serve web traffic, usually the root domain and www. Leave mail records DNS only, because proxying mail breaks delivery. If your site uses a subdomain for an API or an admin panel that must reach the origin directly, keep that one grey too, but protect it another way, such as IP allowlisting on the server. Test each subdomain after you flip it: load the site, check the response headers for a cf-cache-status header, and confirm the page still works over HTTPS.
Set the SSL mode so HTTPS never breaks
SSL mode is where most Cloudflare setups go wrong. The dashboard offers Flexible, Full, and Full (Strict). Flexible encrypts traffic between the visitor and Cloudflare but talks plain HTTP to your origin. It looks secure in the browser while leaving the last hop open, so avoid it for anything that handles logins or payments. Full encrypts both hops but accepts any certificate on your origin, including a self-signed one. Full (Strict) also encrypts both hops and requires a valid certificate on the origin.
Use Full (Strict) with a free certificate on your VPS. Certbot from Let’s Encrypt issues one in minutes, and our guide to free HTTPS on Nginx from install to auto-renewal walks through the install. After you switch modes, test with curl and check the certificate chain from the outside. A redirect loop usually means the origin still forces HTTP to HTTPS while Cloudflare also rewrites, so check the Nginx redirect rules before you blame DNS.
Lock the origin so attackers cannot skip Cloudflare
A proxy only protects you if traffic cannot go around it. Your VPS IP address is public knowledge once the site has run without a proxy, and anyone can connect to it directly, bypassing every Cloudflare rule. Close that gap in the firewall. Allow ports 80 and 443 only from Cloudflare IP ranges, and block direct access from the rest of the internet. Cloudflare publishes its IP ranges, and you should pull the current list when you write the rules instead of copying an old list from a blog post.
On Ubuntu with UFW the pattern looks like this:
sudo ufw allow from 173.245.48.0/20 to any port 443 proto tcp sudo ufw allow from 103.21.244.0/22 to any port 443 proto tcp sudo ufw deny 443/tcp sudo ufw reload
Repeat for each Cloudflare range and for port 80, then test that your domain still loads while a direct request to the VPS IP address fails. Keep SSH open only for your own IP address, and follow the rest of the steps in our guide to setting up a secure Ubuntu 24.04 VPS from scratch so the origin stays hardened even if someone finds its address. As a second layer, enable Authenticated Origin Pulls, which makes your web server reject any connection that does not carry a Cloudflare client certificate.
Add WAF rules that block real attacks
With the proxy and SSL in place, turn on the managed ruleset in the WAF section. Cloudflare ships rules that cover common attack patterns, including SQL injection, cross-site scripting, and known exploit paths. On the free plan you get a managed ruleset with a limited set of custom rules, which is enough for most small sites. Start in a monitoring mindset: enable the rules, watch the events log for a day, and check which legitimate requests get flagged before you tighten anything. The cloudflare waf rules vps owners need most are the managed ruleset plus a few custom rules of their own.
Then add a few custom rules of your own. Block requests to wp-login.php or admin paths from countries you never serve, rate-limit the login page to a handful of requests per minute per IP, and challenge traffic with a bad reputation score. A comparison of managed WAF options for small sites is in our guide to the best WAF for small sites, Cloudflare vs Sucuri, if you want to see how the free tier stacks up against a paid alternative. Remember that a WAF is a filter, not a patch: keep your apps updated, because a rule can only block patterns it knows.
Cache, test, and keep an eye on it
Turn on caching for static files so Cloudflare serves images, CSS, and JavaScript from its edge instead of asking your VPS each time. Set a browser cache TTL that matches how often your assets change, and use cache rules to skip caching for logged-in pages, carts, and API responses. Purge the cache after every deploy that changes static files, or visitors will see the old version until the TTL expires.
Finish with a short test round. Load the site from a fresh browser profile, run a DNS lookup and confirm the answer points to Cloudflare addresses, check the response headers for caching, and try a blocked request to see the WAF page. If you run other providers too, compare DNS hosts on price, anycast coverage, and free-tier limits before you decide whether Cloudflare stays the right DNS host for you. Once the tests pass, put a monthly reminder in your calendar to review the WAF events log, refresh the origin firewall rules, and confirm the certificate renews on its own.