A high-performance static file server built with Rust, designed to efficiently serve static files and images with advanced caching and optimization features.
- Static File Serving: Serves CSS, JS, and other static files with intelligent caching
- Dynamic Image Processing: On-demand image resizing and WebP conversion
- Multi-Host Support: Serve content from multiple origin hosts with automatic host detection
- Smart Caching: Fetches files from origin servers and caches them locally for improved performance
- Automatic Resizing: Resize images on-the-fly using width parameters (e.g.,
w100,w300) - WebP Conversion: Convert images to WebP format for better compression
- AVIF Conversion: Convert images to AVIF format for even better compression and quality
- Format Detection: Automatically detect and handle various image formats (PNG, JPG, JPEG, GIF, WebP, AVIF, SVG)
- Quality Optimization: Serve optimized images based on request parameters
The CDN now supports serving content from multiple hosts with intelligent path parsing:
- URL-based requests:
https://example.com/images/path/to/image.jpg - Absolute paths:
/images/path/to/image.jpg - Relative paths:
images/path/to/image.jpg
/
├── src/
│ ├── controllers/ # Request handlers
│ ├── services/ # Business logic
│ ├── utils/ # Utility functions
│ ├── env/ # Environment configuration
│ └── constants/ # Application constants
├── cdn_root/ # Root directory for cached files, customizable by environment variable `CDN_ROOT`
│ ├── files/ # Cached static files
│ └── images/ # Cached and processed images
├── config/
│ └── nginx.conf # Nginx configuration example
└── docker-compose.yml # Docker deployment
- Rust (latest stable)
- Docker (for containerized deployment)
- Basic understanding of Nginx (for production setup)
sudo apt install pkg-config libssl-devThe reqwest library requires pkg-config and libssl-dev packages for HTTP client functionality.
cargo runThe server will start listening on the configured address (default: 127.0.0.1:41890).
- Endpoint:
/files/*path - Example:
http://localhost:41890/files/css/style.css
- Endpoint:
/images/*path - Resize:
http://localhost:41890/images/w100/photo.jpg(resizes to 100px width) - WebP conversion:
http://localhost:41890/images/photo.jpg.webp - AVIF conversion:
http://localhost:41890/images/photo.jpg.avif
- Full URL:
http://localhost:41890/images/https://example.com/images/logo.png - Host-based:
http://localhost:41890/images/example.com/logo.png
Environment variables for customization:
BIND_ADDRESS: Server bind address (default:127.0.0.1)PORT: Server port (default:41890)HOST: Default origin host (default:http://localhost/)ALLOWED_HOSTS: Comma-separated list of allowed hosts for whitelisting (e.g.,example.com,cdn.example.org). If empty or not set, all hosts are allowed
docker-compose up -dFor optimal performance in production, deploy behind Nginx using the provided configuration in config/nginx.conf.
Set up cron jobs to automatically clean up unused files:
# Remove CSS/JS files older than 5 days
00 4 * * * /usr/bin/find /path/to/cdn_root -mindepth 2 -atime +5 -type f \( -o -iname \*.css -o -iname \*.js \) | xargs rm 1>/dev/null 2>/dev/null
# Remove media files older than 1 year
00 4 * * * /usr/bin/find /path/to/cdn_root -mindepth 2 -atime +365 -type f \( -iname \*.png -o -iname \*.jpg -o -iname \*.jpeg -o -iname \*.gif -o -iname \*.webp -o -iname \*.avif -o -iname \*.mp4 -o -iname \*.webm -o -iname \*.svg -o -iname \*.css -o -iname \*.js \) | xargs rm 1>/dev/null 2>/dev/nullcargo testcargo build --releaseThis project is licensed under the MIT License.