How to protect the website on your VPS with Cloudflare (free plan) Print

  • 0

Cloudflare sits in front of your website: visitors talk to Cloudflare, and Cloudflare passes the requests on to your VPS. The free plan already includes protection against DDoS attacks on the website (at no extra charge), a basic firewall, HTTPS and caching of images, CSS and JavaScript. The account is yours, created directly with Cloudflare, at no cost.

What it protects: the website, that is, the HTTP/HTTPS traffic of your domain. It doesn't protect the entire VPS: SSH, Remote Desktop (RDP), game servers, databases and email still connect directly to the VPS's IP, with the basic network protection (L3/L4) of our infrastructure.

Before you start

  • Your website must already open with your domain and with HTTPS (padlock), directly on the VPS. If your VPS came with a ready-made app, first switch its address to your domain (command dphost-apps dominio) with the gray cloud; only then turn on the protection.
  • Have your login ready for the website where your domain was registered (registro.br, Hostinger, GoDaddy, etc.).
  • Write down the DNS records your domain has today (website, email, subdomains). You'll use this list to check step 3.

Step by step

  1. Create your account at dash.cloudflare.com and confirm your email. The dashboard is available in several languages.
  2. Add your domain (Add a domain) and choose the Free plan.
  3. Check the DNS records. Cloudflare copies the records from your current DNS, but it may miss some: compare them one by one with your list and add whatever is missing. Then set the cloud on the A, AAAA and CNAME records (the other record types don't have a cloud):
    • Proxied (orange cloud): only the website addresses, usually the domain (@) and www, pointing to the VPS's IP.
    • DNS only (gray cloud): everything else, especially email: the name the MX record points to (such as mail) and the DKIM CNAME records. Email with an orange cloud stops working. MX and TXT records (SPF, DKIM, DMARC) don't have a cloud; just check that they were copied.
  4. Change your domain's nameservers (DNS) to the two that Cloudflare shows you (something like name.ns.cloudflare.com). You make this change at the website where your domain was registered; at registro.br, log in, click the domain and, in the DNS section, use "Alterar servidores DNS" (change DNS servers). If your domain has DNSSEC turned on, turn it off before the change, or the domain may stop working. Once everything is active, you can turn DNSSEC back on through Cloudflare itself (it shows you a DS record to add at your registrar's website).
  5. Wait for activation. It takes from a few minutes to 24 hours, and Cloudflare lets you know by email. Your website stays online during the change.
  6. Set up HTTPS: under SSL/TLS → Overview, choose the custom mode (Custom SSL/TLS) and select Full (strict). Don't use "Flexible": it leaves the connection between Cloudflare and the VPS unencrypted and often causes the "redirected you too many times" error (ERR_TOO_MANY_REDIRECTS), which is common with WordPress.
  7. Test: open your website (the padlock should appear) and then open https://yourdomain.com/cdn-cgi/trace. If you see a list that includes colo=, your website is going through Cloudflare.

Important precautions

  • SSH using the domain name stops working with the orange cloud, because Cloudflare only passes on website traffic. Connect using the IP: ssh root@YOUR_IP.
  • Panels on other ports (for example, Coolify on port 8000) don't go through Cloudflare: access them using the VPS's IP. The ports that work with the orange cloud are 80, 443, 2052, 2053, 2082, 2083, 2086, 2087, 2095, 2096, 8080, 8443 and 8880.
  • Free plan limits: uploads larger than 100 MB in a single request are rejected (this affects large uploads through the browser), and pages that take more than 125 seconds (about 2 minutes) to respond return error 524 (for example, long imports). For these tasks, use the VPS's IP or a subdomain with the gray cloud.
  • Automatic notifications (webhooks) from payment providers, WhatsApp and other systems may be blocked by the bot protection. Keep Bot Fight Mode and I'm Under Attack mode turned off, except during an attack. If a notification is blocked, see why under Security → Events.
  • Always Use HTTPS can stay off, because your server already sends visitors to HTTPS. If you turn it on, Let's Encrypt validation also starts going over HTTPS, and certificate renewal by certbot in --nginx, --apache or --standalone mode may fail.
  • Cache: Cloudflare only caches files such as images, CSS and JavaScript; the pages still come from the VPS. Don't create a rule that caches everything (Cache Everything) on a website with logins, a store or a members area: the page with one visitor's data may be shown to another.
  • Hiding the VPS's IP: any gray-cloud record pointing to the same IP (such as mail or ftp) reveals the VPS's address, and anyone who knows the IP can attack it directly, bypassing Cloudflare. See below how to close this "back door".

See visitors' real IP addresses (recommended)

With Cloudflare in front, the VPS sees Cloudflare's IP addresses instead of the visitors', in the logs and in security plugins. A plugin that blocks IPs may end up blocking Cloudflare itself and, with it, many visitors at once. To fix this, as root:

  • Nginx:
    { for ip in $(curl -fsS https://www.cloudflare.com/ips-v4) $(curl -fsS https://www.cloudflare.com/ips-v6); do echo "set_real_ip_from $ip;"; done; echo "real_ip_header CF-Connecting-IP;"; } > /etc/nginx/conf.d/cloudflare.conf
    nginx -t && systemctl reload nginx
    If the test shows an error, undo it with rm /etc/nginx/conf.d/cloudflare.conf.
  • Apache:
    { echo "RemoteIPHeader CF-Connecting-IP"; for ip in $(curl -fsS https://www.cloudflare.com/ips-v4) $(curl -fsS https://www.cloudflare.com/ips-v6); do echo "RemoteIPTrustedProxy $ip"; done; } > /etc/apache2/conf-available/cloudflare.conf
    a2enmod remoteip && a2enconf cloudflare && apache2ctl configtest && systemctl restart apache2
    If the test shows an error, undo it with a2disconf cloudflare.
  • WordPress and ready-made apps: in security plugins (such as Wordfence), choose the option to get the visitor's IP from the CF-Connecting-IP header.
  • Panels (CloudPanel, Coolify and others): follow the panel's instructions for Cloudflare.
  • Windows (IIS 8.5 or newer): in IIS Manager, click the site → Logging → W3C format → Select Fields → Add Field, with Source Type Request Header and Source CF-Connecting-IP. This records the real IP only in the log; the application still sees Cloudflare's IP.

Cloudflare's IP list rarely changes. Run the command again from time to time, for example every 3 months.

Close the "back door" (optional, advanced)

Anyone who already knows the VPS's IP can still attack the website directly. To close this door, allow ports 80 and 443 only from Cloudflare in the VPS firewall. Here is an example with Ubuntu's ufw, for a website on Nginx or Apache installed directly on the system.

Warning: ufw enable blocks all other incoming ports. If the VPS runs other services (email, FTP, a panel, a game), allow each port first (for example, ufw allow 587/tcp). If you changed the SSH port, replace OpenSSH with your port (for example, ufw allow 2222/tcp), or you'll lose access. When ufw enable asks, answer y. If you lose access, log in through the console (VNC) in the client area and run ufw disable.

ufw allow OpenSSH
for ip in $(curl -fsS https://www.cloudflare.com/ips-v4) $(curl -fsS https://www.cloudflare.com/ips-v6); do ufw allow proto tcp from $ip to any port 80,443; done
ufw enable

Run ufw status and check that several lines with 80,443/tcp and Cloudflare's addresses appear. Only then delete the rules that allow 80 and 443 from everyone (Anywhere), if they exist: ufw delete allow 80/tcp, ufw delete allow 443/tcp and, if they appear, ufw delete allow 'Nginx Full' or ufw delete allow 'Apache Full'. Check again with ufw status. Run the for command again every 3 months (duplicate rules are skipped).

  • If the website uses Docker (like the ready-made apps and Coolify), ufw doesn't apply to ports opened by Docker. In that case, don't use these commands.
  • On CloudPanel, use the panel's own firewall instead of ufw.
  • With the door closed, if you ever take Cloudflare out of the way (gray cloud), the website stops working until you allow 80 and 443 again: ufw allow 80/tcp and ufw allow 443/tcp.

How to undo it: in Cloudflare, set the website's cloud to gray (DNS only); within a few minutes, visitors go straight to the VPS again. If you closed the back door, allow 80 and 443 first. To leave Cloudflare for good: if you turned on DNSSEC there, first delete the DS record at the website where your domain was registered and wait 2 days; check that your DNS records still exist at your previous DNS provider; only then switch back to the previous nameservers.

Paid plans: the free plan is enough for most websites. Cloudflare's paid plans are billed per domain, in US dollars, and purchased directly from Cloudflare, in your account. DP HOST does not sell Cloudflare plans.

Would you rather have us do it? DP HOST sets everything up and hands it over working, for a one-time fee of US$ 51.99 (1 domain): see Cloudflare setup by DP HOST. Customers in Brazil pay in Brazilian reais (BRL): see the Portuguese version of this article.

Support: if you do it yourself, your Cloudflare account and its configuration are your responsibility (the VPS is unmanaged). Our support team helps with questions on the VPS side.


Was this answer helpful?

« Back

Powered by WHMCompleteSolution