To serve images from your private Wasabi bucket (media.locacheck.de) using Cloudflare's CDN, you'll need to configure both Wasabi and Cloudflare to work seamlessly together. Below are the step-by-step instructions to achieve this setup:
1. Prerequisites
Before you begin, ensure you have:
- Wasabi Account: Access to your Wasabi account with permissions to manage buckets and policies.
- Cloudflare Account: An active Cloudflare account.
- Domain Access: Control over your domain's DNS settings.
2. Configure Your Wasabi Bucket
a. Understand Your Bucket's Endpoint
Wasabi buckets follow the S3-compatible URL structure. For the EU Central 2 region, the endpoint typically looks like:
https://media.locacheck.de.s3.eu-central-1.wasabisys.com
Note: Replace eu-central-1 with your specific region if different.
b. Set Bucket Policy to Restrict Access to Cloudflare
To keep your bucket private and allow access only through Cloudflare, you'll need to set a bucket policy that permits requests only from Cloudflare's IP ranges.
-
Retrieve Cloudflare's IP Ranges:
Cloudflare publishes its IP ranges here. You'll need both IPv4 and IPv6 addresses.
-
Create a Bucket Policy:
Navigate to your Wasabi bucket settings and locate the Bucket Policy section. Insert a policy that looks like the following, replacing YOUR_BUCKET_NAME and adding all Cloudflare IP ranges:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": [
"173.245.48.0/20",
"103.21.244.0/22",
"103.22.200.0/22",
"103.31.4.0/22",
"141.101.64.0/18",
"108.162.192.0/18",
"190.93.240.0/20",
"188.114.96.0/20",
"197.234.240.0/22",
"198.41.128.0/17",
"162.158.0.0/15",
"104.16.0.0/13",
"104.24.0.0/14",
"172.64.0.0/13",
"131.0.72.0/22"
]
}
}
}
]
}
Important:
- Replace
YOUR_BUCKET_NAME with media.locacheck.de.
- Include all IPv4 and IPv6 ranges provided by Cloudflare.
- Regularly update this policy as Cloudflare may change their IP ranges.
-
Save the Policy:
Apply and save the policy to enforce these restrictions.
3. Set Up Cloudflare for Your Subdomain
a. Add Your Domain to Cloudflare
- Log in to your Cloudflare account.
- Add a Site by entering your main domain (
locacheck.de).
- Follow the prompts to update your domain’s nameservers to the ones provided by Cloudflare. This step delegates DNS management to Cloudflare.
b. Configure DNS for media.locacheck.de
-
Navigate to the DNS Settings:
In your Cloudflare dashboard, go to the DNS section for locacheck.de.
-
Add a CNAME Record:
- Name:
media (This represents media.locacheck.de)
- Target: Your Wasabi bucket endpoint, e.g.,
media.locacheck.de.s3.eu-central-1.wasabisys.com
- Proxy Status: Proxied (Ensure the orange cloud icon is active)
[image: Cloudflare CNAME Setup] open (Replace with an actual image if possible)
-
Save the Record:
Confirm and save the CNAME record.
c. Enable SSL/TLS
-
Navigate to SSL/TLS Settings:
Go to the SSL/TLS section in Cloudflare.
-
Select SSL Mode:
- Full: Encrypts the connection between the user and Cloudflare and between Cloudflare and Wasabi.
- Full (Strict): Requires a valid SSL certificate on the origin (Wasabi).
Recommendation: Use Full (Strict) for enhanced security.
-
Verify SSL Certificate on Wasabi:
Ensure that Wasabi has a valid SSL certificate for your bucket domain (media.locacheck.de). Wasabi typically manages SSL certificates for bucket endpoints, but verify to ensure compatibility with Cloudflare's SSL settings.
4. Optimize Cloudflare Settings for Image Delivery
a. Caching
-
Navigate to the Caching Section:
Go to the Caching tab in your Cloudflare dashboard.
-
Set Cache Level:
- Standard is usually sufficient, but you can choose Aggressive if appropriate.
-
Configure Browser Cache Expiration:
Adjust according to how frequently your images change. For static images, a longer expiration is beneficial.
-
Enable “Always Online”:
This ensures that a cached version of your site is served even if the origin is unreachable.
b. Page Rules (Optional)
Create specific caching or performance rules for media.locacheck.de:
-
Navigate to Page Rules:
In Cloudflare, go to the Page Rules section.
-
Create a New Rule:
- URL:
media.locacheck.de/*
- Settings: Customize caching, image optimization (like Polish), and other performance settings as needed.
-
Save and Deploy:
Apply the settings to optimize content delivery for your media subdomain.
5. Test Your Setup
a. Upload an Image to Wasabi
- Upload a test image to your Wasabi bucket (
media.locacheck.de).
b. Access the Image via Cloudflare
- Open a browser and navigate to
https://media.locacheck.de/your-image.jpg.
- The image should load correctly, served through Cloudflare's CDN.
c. Verify Direct Access is Denied
- Try accessing the image directly via Wasabi's endpoint, e.g.,
https://media.locacheck.de.s3.eu-central-1.wasabisys.com/your-image.jpg.
- You should receive an AccessDenied error, confirming that direct access is restricted.
6. Maintain and Monitor
a. Monitor Access Logs
- Regularly check access logs in Wasabi and Cloudflare to ensure that the configuration is working as intended.
b. Update Cloudflare IP Ranges
- Periodically verify and update Cloudflare's IP ranges in your Wasabi bucket policy, as Cloudflare may add or modify their IP addresses.
c. Optimize Further
- Use Cloudflare features like Image Optimization, Caching Level adjustments, and HTTP/2 or HTTP/3 to enhance performance.
Alternative Approach: Using Cloudflare Workers for Signed URLs
If restricting access based on IP ranges is not feasible or you require more granular control, you can use Cloudflare Workers to generate signed URLs for accessing private Wasabi content.
a. Overview
- Cloudflare Workers act as serverless functions that can intercept requests and modify responses or perform actions like signing URLs.
- This approach ensures that only authenticated requests through Cloudflare can access your Wasabi bucket.
b. Steps
-
Develop a Worker Script:
Write a Cloudflare Worker that:
- Intercepts requests to
media.locacheck.de.
- Generates signed URLs for accessing private Wasabi objects on-the-fly.
- Redirects or proxies the request using the signed URL.
-
Deploy the Worker:
- Attach the Worker to your
media.locacheck.de/* route in Cloudflare.
-
Update DNS and Testing:
- Ensure DNS settings point to the Worker.
- Test accessing images to verify that signed URLs are correctly generated and served.
c. Benefits
- Enhanced Security: Fine-grained access control without exposing IP ranges.
- Flexibility: Customize URL signing logic as needed.
- Scalability: Serverless execution ensures optimal performance.
Note: Implementing Workers requires familiarity with JavaScript and Cloudflare's Worker environment. Refer to Cloudflare Workers Documentation for detailed guidance.
By following the above steps, you can effectively utilize Cloudflare's CDN to serve images from your private Wasabi bucket securely and efficiently. This setup ensures that your images are cached globally for faster delivery while maintaining strict access controls to your storage.
If you encounter any issues or need further customization, consider consulting Cloudflare's and Wasabi's support documentation or reaching out to their support teams for specialized assistance.