Back to articles
DevOps & Systems

Advanced Cloudflare Tunnels Configuration for VPS Protection: Blocking IP Countries and Integrating Cloudflare Access

Detailed guide on configuring advanced Cloudflare Tunnels to protect VPS, fully block IP addresses from specific countries, and integrate Cloudflare Access for secure,...

DPTCloud Engineering4 min read
Advanced Cloudflare Tunnels Configuration for VPS Protection: Blocking IP Countries and Integrating Cloudflare Access

Advanced Cloudflare Tunnels Configuration for VPS Protection

Cloudflare Tunnels is a powerful tool that allows you to connect your VPS or internal server to the Cloudflare network without exposing public ports. This helps protect your VPS from direct attacks via public IP addresses. In this guide, we will configure advanced Cloudflare Tunnels to fully block IP addresses from specific countries (e.g., Vietnam) and integrate Cloudflare Access for secure, user-controlled access.

Prerequisites and Preparation

Before starting, ensure you have:

  • A Cloudflare account (free or paid)
  • A Linux-based VPS (Ubuntu 20.04 or later)
  • Access to Cloudflare DNS and zone management for your domain
  • Cloudflared installed on your VPS (via the cloudflared package)

Configuring Advanced Cloudflare Tunnels

To protect your VPS and fully block access from specific countries, we will configure Cloudflare Tunnels with advanced security rules.

Step 1: Install cloudflared

Run the following commands on your VPS:

# Install cloudflared
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o /usr/local/bin/cloudflared
chmod +x /usr/local/bin/cloudflared

Step 2: Create a Cloudflare Tunnel

Go to the Cloudflare Dashboard, select your domain, and navigate to Network → Tunnels → Create Tunnel.

Choose a tunnel name (e.g., my-vps-tunnel) and create it. Then download the configuration file or copy the registration command:

cloudflared tunnel login

After logging in, create the tunnel:

cloudflared tunnel create my-vps-tunnel

Step 3: Configure Tunnel with IP Country Blocking Rules

We will use Cloudflare Access to control access and block IP addresses from specific countries (e.g., Vietnam, ISO code VN).

In the Cloudflare Dashboard, go to Access → Gateways → Create Gateway. Name it my-vps-gateway.

Add a blocking rule:

  • Condition: Country is not VN
  • Action: Deny

Apply this rule to the tunnel you created.

Step 4: Connect VPS to the Tunnel

Run the following command on your VPS to start the tunnel:

cloudflared tunnel run my-vps-tunnel

To run the tunnel as a service, create a systemd file:

# Create /etc/systemd/system/cloudflared.service
[Unit]
Description=Cloudflare Tunnel
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/cloudflared tunnel run my-vps-tunnel
Restart=always
User=root

[Install]
WantedBy=multi-user.target

Start the service:

sudo systemctl enable cloudflared
sudo systemctl start cloudflared

Integrating Cloudflare Access for Secure Access

Cloudflare Access enables you to control who can access your VPS through authentication (OAuth, SSO, or password). This is crucial for protecting your VPS from unauthorized access.

Step 1: Configure Access Policy

Create a new access policy in the Cloudflare Dashboard:

  • Name: Access Policy - VPS
  • Condition: IP is not VN → Deny
  • Condition: User is in group admin → Allow

In Cloudflare Tunnels, select your tunnel and go to Access → Enable. Then select your application (e.g., http://vps.mydomain.com) and link it to the access policy you created.

Verification and Testing

After configuration, verify connectivity:

# Check tunnel status
cloudflared tunnel status

# Test external connection
curl -I https://my-vps-tunnel.cloudflareaccess.com

If successful, you will see a 200 OK response from Cloudflare. If blocked, the response will be 403 Forbidden.

Troubleshooting

  • Cannot connect: Check the tunnel log: journalctl -u cloudflared -f
  • IP country not blocked: Verify the Access Policy and geographic settings in the Cloudflare Dashboard.
  • Tunnel not visible in dashboard: Ensure you are logged in with the correct account and the tunnel has been successfully registered.

References

Note: Fully blocking IP addresses from specific countries (e.g., VN) may affect legitimate users. Only apply this in internal environments or when high security is required. Consider carefully before deployment.

Related infrastructure & engineering guides: NVMe Cloud VPS for Docker, Nginx & Databases · The Microservices Revolution: Running WebAssembly (Wasm) Apps Instead of Docker on Low-Config VPS

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478