# Varnish Cache Setup for Bookmarkly This setup implements Varnish Cache as a reverse proxy for Bookmarkly to dramatically improve performance, especially for images. ## Architecture ``` Internet → Varnish Cache (Port 80) → Apache/PHP App (Port 80 internal) ``` ## Features - **Ultra-fast caching** of static assets (images, CSS, JS) - **CDN-like functionality** for external images - **Automatic compression** (Gzip) - **Smart cache invalidation** - **Image optimization** and resizing - **Cache purging** via API ## Quick Start 1. **Start the services:** ```bash docker-compose up -d ``` 2. **Access the application:** - Main app: http://localhost - Varnish admin: http://localhost:6082 ## Configuration ### Varnish Configuration (`varnish/default.vcl`) - **Image caching**: 30 days - **CSS/JS caching**: 7 days - **API proxy caching**: 1 day - **Compression**: Enabled for text-based content - **CORS**: Enabled for images ### Image Proxy (`api/image.php`) Use the image proxy for external images: ``` http://localhost/api/image.php?url=https://example.com/image.jpg&w=800&h=600&q=85 ``` Parameters: - `url`: External image URL - `w`: Width (optional) - `h`: Height (optional) - `q`: Quality 1-100 (optional, default: 85) ### Cache Purging (`api/purge_cache.php`) Purge cache via API (Admin only): ```bash # Purge all cache curl -X POST http://localhost/api/purge_cache.php -d "type=all" # Purge specific image curl -X POST http://localhost/api/purge_cache.php -d "type=image&url=/path/to/image.jpg" # Purge all images curl -X POST http://localhost/api/purge_cache.php -d "type=images" ``` ## Performance Benefits - **10-100x faster** image loading - **Reduced bandwidth** usage - **Lower server load** - **Better user experience** - **CDN-like performance** locally ## Monitoring ### Varnish Statistics ```bash # View Varnish stats docker exec bookmarkly-varnish varnishstat # View Varnish logs docker exec bookmarkly-varnish varnishlog ``` ### Cache Hit Rate Check the `X-Cache` header in responses: - `HIT`: Served from cache - `MISS`: Fetched from backend ## Troubleshooting ### Clear All Cache ```bash docker exec bookmarkly-varnish varnishadm "ban req.url ~ ." ``` ### Restart Varnish ```bash docker-compose restart varnish ``` ### Check Configuration ```bash docker exec bookmarkly-varnish varnishd -C -f /etc/varnish/default.vcl ``` ## Security - **Domain whitelist** for external images - **URL validation** to prevent abuse - **File type restrictions** - **Rate limiting** (can be added) ## Customization ### Add Allowed Domains Edit `api/image.php` and add domains to `$allowedDomains` array. ### Adjust Cache TTL Modify `varnish/default.vcl` to change cache durations. ### Add Compression Varnish automatically compresses text-based content. For more control, modify the VCL configuration. ## Production Considerations 1. **Increase Varnish memory** for high-traffic sites 2. **Add monitoring** (Varnish stats, cache hit rates) 3. **Implement cache warming** for critical images 4. **Add rate limiting** for the image proxy 5. **Consider Varnish Plus** for enterprise features ## File Structure ``` varnish/ ├── default.vcl # Varnish configuration docker-compose.yml # Multi-container setup api/ ├── image.php # Image proxy with caching └── purge_cache.php # Cache purging API .htaccess # Apache caching rules ```