Overview
Coraza Proxy supports flexible backend configuration through theBACKENDS environment variable. You can configure multiple backend servers with host-based routing, path-based routing, and automatic round-robin load balancing.
Backend Configuration Format
TheBACKENDS environment variable accepts two formats:
New Format (Recommended)
The new format supports both host-based and path-based routing:Legacy Format (Compatibility)
The legacy format supports simple host-based routing:Environment Variables
JSON configuration for backend servers. If not set, defaults to
{"default": ["localhost:5000"]}.How It Works
The backend selection follows this logic (frommain.go:317-341):
Selection Process
- Path matching: The proxy first checks if the request path matches any path prefix defined in the
pathsconfiguration - Longest prefix wins: If multiple path prefixes match, the longest one is selected
- Default fallback: If no path matches, the
defaultbackend is used - Host fallback: If no configuration exists for the requested host, the
defaulthost configuration is used
Load Balancing
When multiple backend addresses are configured, the proxy uses round-robin load balancing (frommain.go:552-553):
Data Structures
The backend configuration is parsed into these structures (frommain.go:24-45):
Configuration Examples
Single Backend
Multiple Hosts
Path-Based Routing
Load Balancing with Multiple Servers
Docker Compose Example
Error Handling
If no backend can be selected for a request, the proxy returns:Best Practices
- Always define a default: Include a
"default"host configuration to handle unmatched hosts - Use path prefixes wisely: More specific paths should be listed alongside less specific ones (e.g.,
/static/imagesand/static) - Load balance critical services: Configure multiple backend addresses for high-availability services
- Use Docker DNS: In Docker environments, use service names instead of IP addresses for automatic service discovery
