Rclone: Cloudflare R2 and Nginx Reverse Proxy
samnang rosady

samnang rosady @medrix

About: 👨‍💻 Fullstack dev by day, 🌃 IT server whisperer by night | Sharing my knowledge on frontend, backend, devops, and even a sprinkle of infrastructure.

Location:
Phnom Penh, Cambodia
Joined:
Jul 12, 2024

Rclone: Cloudflare R2 and Nginx Reverse Proxy

Publish Date: Mar 24
0 0

Rclone is a powerful command-line tool that allows you to sync, copy, and manage files across multiple cloud storage providers. Cloudflare R2 is an object storage service designed to provide low-latency, high-availability storage without egress fees. By integrating Rclone with Cloudflare R2, you can efficiently manage your cloud storage with ease.

Installation

sudo yum install epel-release
sudo yum -y install fuse rclone -y
ln -s /bin/fusermount /bin/fusermount3
Enter fullscreen mode Exit fullscreen mode

Rclone config

vim ~/.config/rclone/rclone.conf
Enter fullscreen mode Exit fullscreen mode
  • rclone.conf
[r2demo]
type = s3
provider = Cloudflare
access_key_id = xxx
secret_access_key = xxxx
endpoint = xxxxx
region = auto
acl = private"
Enter fullscreen mode Exit fullscreen mode

Rclone mount R2 remote to local

Option 1 (Debugging):

rclone mount r2demo:<bucket-name> <local-destination> --vfs-cache-mode off --log-file rclone.log  --log-level DEBUG
Enter fullscreen mode Exit fullscreen mode

Option 2:
Run in the background

nohup rclone mount r2demo:<bucket-name> <local-destination> --vfs-cache-mode off --log-file /var/log/rclone.log --log-level NOTICE > /dev/null 2>&1 &
Enter fullscreen mode Exit fullscreen mode

Using --log-file, in case of debugging.

  • --log-level LEVEL

    • DEBUG is equivalent to -vv. It outputs lots of debug info - useful for bug reports and really finding out what rclone is doing.
    • INFO is equivalent to -v. It outputs information about each transfer and prints stats once a minute by default.
    • NOTICE is the default log level if no logging flags are supplied. It outputs very little when things are working normally. It outputs warnings and significant events. ### Symlink
ln -s <local-destination> <project-storage-destination>
Enter fullscreen mode Exit fullscreen mode

Nginx reverse proxy

✅ Faster global delivery due to Cloudflare’s CDN caching.
✅ Potential cost savings by reducing server bandwidth usage.
✅ Decreased load on server, especially for high-traffic websites.

map $uri $new_uploads_uri {
  ~^/<storage-uri>/(.*)$ //$1;
}
server {
  .....
  location ^~ /<storage-uri>/ {
     resolver 1.1.1.1;
     proxy_ssl_server_name on;
     proxy_pass https://<public-r2-domain>$new_uploads_uri;
  }
}
Enter fullscreen mode Exit fullscreen mode

Sample:

  • < storage-uri >: wp-content/upload. We want to reverse from $HOST/wp-content/upload/xxx/xxx/xxx.jpg to <public-r2-domain>/upload/xxx/xxx/xxx.jpg

Conclusion

By combining Rclone, Nginx, and Cloudflare R2, you can serve static files efficiently with:
✅ Custom domain support
✅ SSL encryption
✅ Caching & compression
✅ No egress fees

🌟 Stay tuned 🌟

Comments 0 total

    Add comment