Guide ·

Cloudflare Setup for a VPS Website: DNS, SSL, Caching and Security

How to put a website or API on your own VPS behind Cloudflare: DNS records, Full (strict) SSL, locking the origin, real visitor IPs, caching and webhooks.

By Ritesh Singh, full-stack developer

Cloudflare in front of a VPS gives you fast DNS, free SSL at the edge, caching for static files and protection from floods of bad traffic, and it costs nothing on the free plan. I set it up on most client servers, including the AEPS payments dashboard and a multi-tenant SaaS server, and this site runs on Cloudflare too. A few wrong switches cause redirect loops, blocked webhooks or a server still open to the internet. This is the order I work in.

1. Add the domain and check every DNS record

Add the domain in Cloudflare and let it import the existing records. The import is a guess, not a copy: compare it line by line with your current DNS panel before you change anything. The records most often missed are MX and mail subdomains, TXT records for SPF, DKIM and DMARC, and verification records for Google, Microsoft or a payment provider.

Then change the nameservers at your registrar to the two Cloudflare gives you. If DNSSEC is on at the registrar, turn it off first and turn it back on through Cloudflare once the domain is active, or the domain can stop resolving. If you are also moving the site to a new server, do the steps in my shared hosting to VPS migration guide first, so you change one thing at a time.

2. Decide which records are proxied

An orange-cloud (proxied) record sends traffic through Cloudflare, which hides your server's IP and applies SSL, caching and security rules. Cloudflare's proxy handles web traffic, so proxy only the records that serve your website or API. Keep these DNS-only (grey cloud): mail server records, SSH or admin hostnames, FTP and anything else that is not HTTP. A grey record on the same IP reveals your server's address, so lock that service down in the firewall.

3. Use Full (strict) SSL with a real certificate on the server

There are two connections: visitor to Cloudflare, and Cloudflare to your server. Cloudflare covers the first one automatically. For the second, set the SSL/TLS mode to Full (strict), so the traffic is encrypted and the server's certificate is checked. Never use Flexible: it talks to your server over plain HTTP, and combined with an HTTPS redirect on the server it creates an endless redirect loop.

The server needs a valid certificate. Either issue one from Let's Encrypt (Coolify, Caddy and Certbot do this for you; issue it before turning the proxy on, as I describe in the Coolify deployment guide), or install a free Cloudflare Origin CA certificate, which is valid for years but trusted only by Cloudflare. Then turn on Always Use HTTPS. Add HSTS later, with a short max-age at first, because it is hard to undo once browsers have cached it.

4. Lock the server so only Cloudflare can reach it

Hiding the IP is not protection on its own. Allow ports 80 and 443 only from Cloudflare's published IP ranges, and SSH only from your own IP or over a VPN. If you would rather have no open web ports at all, Cloudflare Tunnel runs a small connector on the server that makes an outbound connection to Cloudflare instead.

One trap catches many Docker setups: ports published with docker run -p or in Docker Compose bypass UFW, because Docker writes its own iptables rules. Publish only your reverse proxy's ports, keep app and database containers on the internal Docker network, and check from outside with a port scan that nothing else answers. My guide to running a Node.js API in Docker shows a setup where only the proxy publishes ports.

5. Restore real visitor IP addresses

Behind the proxy, every request seems to come from a Cloudflare address. That breaks rate limiting, login security, audit logs and analytics. Cloudflare sends the visitor's address in the CF-Connecting-IP header. In Nginx, list Cloudflare's ranges with set_real_ip_from and set real_ip_header CF-Connecting-IP. In a Node.js app, read the header only when the request really came through Cloudflare or your own proxy, otherwise anyone can send a fake one.

6. Cache static files, not personal pages

By default Cloudflare caches static files such as images, CSS, JavaScript and fonts, and passes HTML and API responses through to your server. That default is safe. Give static files long cache lifetimes and versioned names; Next.js already does this for everything under /_next/static/, so those files can be cached for a year.

Cache HTML only for pages that are the same for every visitor. Exclude admin areas, logged-in pages, carts and every API path, and send Cache-Control: private, no-store from the API for anything personal. After a deploy that changes un-versioned files, purge those URLs from the cache.

7. Security rules that do not block your own integrations

Turn on the managed WAF rules your plan includes, and add a rate limiting rule for login, OTP and contact form endpoints, keeping limits in the app as well. Be careful with bot challenges: a payment gateway webhook, a bank callback or a Flutter app calling your API cannot solve a challenge page. Add a rule that skips challenges for those exact paths, and protect them in the app instead by verifying the provider's signature, as in my Razorpay webhook guide.

Also note which IP your providers see. Calls your server makes to a bank or payment API go out directly from the server, not through Cloudflare, so the provider whitelists your server's IP. That is why I keep bank API integrations on a VPS with a fixed address.

8. Email, uploads and long requests

Cloudflare does not host mailboxes. Keep the MX records pointing at your mail provider; if you only need addresses such as hello@yourdomain.com forwarded to an existing inbox, Cloudflare Email Routing can do that. Two proxy limits matter for apps: request bodies are capped at 100 MB on the Free and Pro plans, and a request that takes longer than about 100 seconds ends in a 524 error. Upload large files straight to object storage, and run slow work such as reports or bulk imports as background jobs.

Common Cloudflare errors and what they usually mean

  • Too many redirects: SSL mode is Flexible while the server redirects to HTTPS. Switch to Full (strict).
  • 521: the server refused the connection, often because the firewall blocks Cloudflare's ranges or the web server is down.
  • 522: the connection timed out, usually a firewall drop or an overloaded server.
  • 525 / 526: SSL handshake failed or the origin certificate is invalid or expired.
  • 524: the app took too long to answer; move the work to a background job.

Checklist

  • Imported DNS records checked against the old panel, including MX and TXT.
  • Only web records proxied; mail and SSH DNS-only.
  • SSL mode Full (strict) with a valid certificate on the server.
  • Ports 80/443 open only to Cloudflare; Docker ports checked from outside.
  • Real visitor IPs restored in Nginx and the app.
  • No caching of admin, logged-in or API responses.
  • Webhook and app API paths skip bot challenges and verify signatures.
  • Uploads and slow jobs designed around the proxy limits.

Want your app on a server you own, with Cloudflare set up properly in front of it? See my VPS setup and deployment service, or send a short brief and I will reply with a plan.

In practice

Case studies using this approach

Keep reading

Related guides

Related services

Hire me for this

Want this done for your product?

Start your project →