How to Secure and Stream Mega.nz WebDAV Content in Kodi via Cloudflare Zero Trust


If you want to securely access and stream your Mega.nz cloud storage via WebDAV using Cloudflare Zero Trust, while ensuring seamless playback in Kodi, this guide is for you. By leveraging Cloudflare Tunnel, WebDAV via MEGAcmd, and secure access policies, you can stream content efficiently while maintaining strong security.
Why Use This Setup for Kodi?
✅ Stream Mega.nz Content Directly in Kodi – Eliminates the need to download files before playing.
✅ Increased Security – Protects your Mega.nz WebDAV access with Cloudflare Zero Trust and IP-based restrictions.
✅ No Local Hosting Required – Uses MEGAcmd’s built-in WebDAV server, eliminating the need for an additional self-hosted WebDAV service.
✅ Seamless Playback in Kodi – Enables buffering, seeking, and smooth playback of high-quality media.
✅ Bypass ISP Throttling – Cloudflare’s CDN can reduce ISP interference while ensuring a reliable connection.
✅ Easy Access Anywhere – No need to expose your WebDAV service to the open internet.
Step 1: Setting Up WebDAV for Mega.nz Using MEGAcmd
Mega.nz does not natively provide WebDAV access, but you can enable it using MEGAcmd.
1️⃣ Install MEGAcmd
MEGAcmd cannot be installed via package managers. Instead, download it from:
2️⃣ Log In to Your Mega Account
Before running WebDAV, authenticate your account:
mega-login your-email@example.com "password"
You’ll be prompted for your password.
3️⃣ Start the WebDAV Server
Run the following command to expose your Mega.nz storage over WebDAV:
mega-webdav Movies
Movies
is the Mega.nz folder you want to share.The WebDAV server will now run locally on http://127.0.0.1:4443.
Example Output of mega-webdav
Without Parameters
If you run mega-webdav
without specifying a folder, it will display the served locations:
mega-webdav
WEBDAV SERVED LOCATIONS:
/Movies: http://127.0.0.1:4443/XyZ123AB/Movies
Step 2: Setting Up Cloudflare Tunnel to Secure WebDAV
The next step is to create a Cloudflare Tunnel to securely route traffic from mega.example.com to your local WebDAV server.
1️⃣ Create and Configure the Tunnel in Cloudflare Zero Trust
Go to Cloudflare Dashboard → Zero Trust → Access → Tunnels.
Click "Create a Tunnel".
Follow the steps to install and connect the Cloudflare daemon (
cloudflared
) on your server.In the "Public Hostname" section, add:
Subdomain:
mega.example.com
Service:
http://127.0.0.1:4443
Save and deploy the tunnel.
Step 3: Securing WebDAV Access with Cloudflare Zero Trust
1️⃣ Create Access Policies
Go to
Cloudflare Dashboard → Zero Trust → Access → Applications
.Click Create an Application and set:
Application Name:
mega.example.com
Session Duration: 1 day (or preferred duration).
Add Access Rules:
✅ Allow IP Address is
192.0.2.123
.✅ Allow Device is enrolled in Cloudflare WARP.
❌ Block All Others.
This ensures only your devices can access WebDAV while keeping it public-facing.
Step 4: Configuring Cloudflare for Smooth WebDAV Streaming in Kodi
By default, Cloudflare may block range requests, which can cause buffering failures in Kodi.
1️⃣ Adjust Cloudflare Tunnel Settings to Prevent Playback Drops
You may experience video playback dropping after 20-30 minutes due to Cloudflare incorrectly assuming your connection has become idle.
Idle Connection Expiration Time (Default: 90s) → Increase to 1200s (20 minutes)
Problem: If the WebDAV stream is idle for 90 seconds, Cloudflare considers the connection inactive and closes it.
Fix: Increase the timeout to 1200s to ensure Cloudflare doesn't drop connections when Kodi buffers video.
TCP Keep Alive Interval (Default: 30s) → Reduce to 15s
Problem: Cloudflare sends keepalive packets every 30 seconds, but slow WebDAV responses may still trigger a disconnect.
Fix: Lowering this to 15s ensures Cloudflare actively keeps the connection alive during playback.
2️⃣ Create a Cloudflare Cache Rule to Exclude Mega.nz from Caching
To ensure WebDAV functions properly in Kodi, you need to bypass caching for mega.example.com:
Go to Cloudflare Dashboard → Caching → Cache Rules.
Click "Create a Cache Rule".
Set the Rule Name:
mega.example.com
.In "If incoming requests match..." select:
- All incoming requests (Apply the rule to all requests).
In "Then..." choose:
- Cache eligibility: Bypass cache.
Save and Apply the rule.
3️⃣ Allow WebDAV HTTP Methods in Cloudflare WAF
Go to Cloudflare Dashboard → Security → WAF.
Create a Custom Rule:
If Request Method is
GET
AND Header ContainsRange: bytes=
Then: Allow.
4️⃣ Verify Fix with curl
To test if range requests are now working, run:
curl -v -X GET -H "Range: bytes=0-1024" https://mega.example.com/XyZ123AB/Movies/movie.mkv
✅ Expected Response: 206 Partial Content
(Success).
Step 5: Connecting WebDAV to Kodi
1️⃣ Add WebDAV Source in Kodi
Go to:
Settings (⚙️) → File Manager
.Click "Add source".
Select Source Type:
DAVS (WebDAV Secure)
.Enter the following details:
Source:
mega.example.com
Port:
443
Partial Path:
XyZ123AB/Movies
Username: (leave blank)
Password: (leave blank)
Click OK → Give it a name → Save.
Verify that you can browse your WebDAV files inside Kodi’s Videos → Files
section.
Subscribe to my newsletter
Read articles from Vadim Kononov directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Vadim Kononov
Vadim Kononov
I am an accomplished Solution Architect, Full Stack Developer and DevOps Specialist with a passion for creative leadership and mentorship, business optimization and technical direction, and ingenious solutions to complex problems. I am especially interested in App & Web Development, Cyber Security, Cloud Computing, Data Science, Open Source Software, Statistical Analysis and Discrete Mathematics.