Building a fast website on a VPS is not only about adding more CPU or RAM; it is mainly about delivering your site’s files from the nearest location to the visitor, while your VPS only handles the parts that must run on the server. This guide will show you how to configure a CDN and Edge Rules for VPS websites.
In this tutorial from PerLod Hosting, we want to use Ubuntu 24.04 LTS, Nginx, and Cloudflare’s edge network to enhance your site's load speed, reduce VPS load, and improve real user performance.
Prepare VPS Server for CDN and Edge Rules Setup
Before installing the software packages and configuring your server, you must prepare your VPS.
Tip: If you need a reliable VPS server, our VPS plans are designed for fast Nginx performance and work perfectly with Cloudflare CDN and Cache Rules.
You can connect to your VPS IP as the root user and run the system update and upgrade:
Bash
sshroot@your_server_ipaptupdate&&aptupgrade-y
Then create a sudo user with the commands below:
Bash
adduserwebadminusermod-aGsudowebadmin
To secure SSH access, it is recommended to disable root login and password authentication. On your local computer, run the command below to SSH in without a password:
Bash
ssh-copy-idwebadmin@your_server_ip
From your VPS, open the SSH config file with the command below:
Bash
nano/etc/ssh/sshd_config
Look for the following lines and adjust the values as shown below:
Bash
PermitRootLoginnoPasswordAuthenticationno
Restart SSH to apply the changes:
Bash
systemctlrestartssh
Also, allow the required ports through your UFW firewall and enable it:
Bash
ufwallowOpenSSHufwallow'Nginx Full'ufwenable
At this point, you can install the LEMP stack so your server can reliably serve web pages, run dynamic PHP applications, and store website data in a database.
With your sudo user, run the commands below to install and enable Nginx:
Do not forget that you need a domain’s A record pointed to the VPS IP and set to Proxied (orange cloud).
Nginx and Cloudflare Optimization
Now you can configure Nginx to work correctly behind Cloudflare. Because Cloudflare acts as a reverse proxy, your VPS will normally see Cloudflare IPs in logs unless you restore the real visitor IP using Cloudflare’s CF-Connecting-IP header and Nginx real IP settings.
Also, you can set strong cache headers for static files so browsers and Cloudflare can safely cache them longer, reducing repeated requests to your VPS and improving load times worldwide.
Nginx must know that Cloudflare is a trusted proxy to log real visitor IPs. Open the following file:
Then, you can connect to your database engine and create the database for your app, for example, WordPress:
Bash
sudomariadb-uroot-p
From your MariaDB shell, you can run:
SQL
1CREATEDATABASE app_db;2CREATE USER 'app_user'@'localhost' IDENTIFIED BY'StrongPassword123!';3GRANTALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';4FLUSH PRIVILEGES;5EXIT;
Secure HTTPS Between Cloudflare and VPS
In this step, you can configure SSL the right way so traffic is encrypted not only between the visitor and Cloudflare, but also between Cloudflare and your VPS. This prevents SSL errors, redirect loops, and insecure setups, while ensuring Cloudflare can safely proxy and accelerate your site over HTTPS.
To do this, you can use a valid SSL certificate on the VPS via Let's Encrypt and set Cloudflare’s SSL mode to use that certificate so both sides of the connection stay protected.
Install Certbot with the command below:
Bash
sudoaptinstallcertbotpython3-certbot-nginx-y
Get your SSL certificates for your domain name with the command below:
Note: Ensure your DNS points to the server IP before running the above command.
Now that the VPS has SSL, you can configure Cloudflare to use it.
From the Cloudflare dashboard settings, navigate to SSL/TLS, Overview, and set it to Full (Strict).
Note: Never use Flexible when the VPS has an SSL certificate. It causes redirect loops.
Also, from the DNS section, ensure the Proxy status (orange cloud) is enabled.
Advanced Cloudflare Edge Rules: Force Cache at the Edge
At this point, you can use Cloudflare Cache Rules to control exactly what gets cached on Cloudflare’s edge servers and what must always come from your VPS.
Cloudflare Cache Rules let you mark requests as Eligible for cache or Bypass cache, then set an Edge TTL and a Browser TTL.
The goal is to cache static files aggressively, so Cloudflare serves them fast from the edge instead of requesting them from your VPS. At the same time, you should bypass caching for admin pages and other sensitive or dynamic parts of the site.
Cloudflare also lets you force caching by setting the Edge TTL to “Ignore cache-control header and use this TTL,” which means Cloudflare will cache those matching files even if your VPS sends different cache headers.
From the Cloudflare dashboard, navigate to:
Caching > Cache Rules > Create Rule
Rule 1: Bypass Admin, which protects your backend from being cached.
Name: Do Not Cache Admin
Expression: URI Path contains /wp-admin OR URI Path contains /wp-login.php
Action: Bypass cache.
Rule 2: Aggressive Static Cache, which forces images or CSS to stay on Cloudflare's servers for 1 month.
Name: Force Static Cache
Expression: File Extension is in jpg, jpeg, png, webp, css, js, svg, woff2
Action: Eligible for cache.
Edge TTL: Ignore cache-control header = 1 Month.
Browser TTL: Override origin = 1 Year.
Verify CDN and Edge Rules for VPS Websites
Now that the VPS, Nginx, SSL, and Cloudflare rules are in place, the next step is to verify each layer is working as expected.
To test the real IP, you can check the logs on the VPS:
Bash
tail-f/var/log/nginx/yourdomain.access.log
Visit the site, and you should see your actual ISP IP address, not a Cloudflare IP.
Conclusion
Now you have successfully built a CDN and Edge Rules for VPS Websites infrastructure. Your VPS is no longer handling repetitive requests for static files. Cloudflare's edge network does that from 300+ cities worldwide. This means faster load times and reduced server CPU usage.
We hope you enjoy this CDN and Edge Rules for VPS Websites guide. Subscribe to our X and Facebook channels to get the latest articles.
This happens when Cloudflare is set to "Flexible" SSL, but your Nginx server is configured to force HTTPS. You must set it to Full (Strict).
Run curl -I https://yourdomain.com/image.jpg in your terminal. If you see cf-cache-status: HIT, it's working. If you see DYNAMIC, check your Cache Rules configuration to ensure the file extension is listed in the "Force Static Cache" rule.
Cloudflare will continue serving cached static assets to visitors, but dynamic pages will fail with a 502 error.
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.