Overview
Theproxy middleware provides reverse proxy functionality, forwarding requests to upstream servers. It supports load balancing, request modification, and response handling.
Installation
Quick Start
Configuration
Examples
Basic Proxy
Path Rewriting
Load Balancing
Request Modification
Response Modification
Preserve Host Header
Custom Timeout
API Reference
Headers Added
Technical Details
Request Flow
-
URL Construction: The target URL is built by combining the upstream target with the request path. If a
Rewritefunction is provided, it transforms the path before joining. -
Path Joining: The
singleJoiningSlashhelper ensures proper path construction by handling slash prefixes/suffixes correctly, preventing issues like double slashes or missing slashes. - Request Creation: A new HTTP request is created with the same method, context, and body as the original request.
-
Header Propagation: All headers from the original request are copied to the proxy request using the
copyHeadersfunction, which preserves multi-value headers. -
Forwarded Headers: The middleware automatically sets standard forwarding headers:
X-Forwarded-For: Appends client IP to any existing value (creates a chain)X-Forwarded-Host: Sets the original hostX-Forwarded-Proto: Setshttpsorhttpbased on TLS presence
-
Host Header Handling: By default, the Host header is set to the upstream targetβs host. When
PreserveHostis true, the original Host header is preserved. -
Request Modification: If
ModifyRequestis provided, itβs called before sending the request, allowing custom header manipulation or other modifications. -
HTTP Client Configuration:
- Uses configurable transport (defaults to
http.DefaultTransport) - Applies the specified timeout (defaults to 30 seconds)
- Disables automatic redirect following (
CheckRedirectreturnsErrUseLastResponse)
- Uses configurable transport (defaults to
-
Response Handling: After receiving the upstream response:
ModifyResponsecallback is invoked if configured- Response headers are copied to the client
- Status code is written
- Response body is streamed to the client using
io.Copy
-
Error Handling: Errors are passed to the custom
ErrorHandlerif configured, otherwise returns a 502 Bad Gateway response.
Load Balancer Implementation
TheBalancer function implements simple round-robin load balancing:
- Maintains a counter for selecting the next upstream
- Uses modulo arithmetic to cycle through targets
- Counter is not thread-safe but provides good-enough distribution for most use cases
- Each request creates a new proxy middleware instance with the selected target
Performance Considerations
- Streaming: Response body is streamed using
io.Copy, avoiding buffering large responses in memory - Connection Reuse: Uses
http.DefaultTransportby default, which includes connection pooling - Context Propagation: Request context is preserved, enabling timeout and cancellation propagation
- Header Copying: Headers are copied efficiently without additional allocations for single-value headers
Best Practices
- Set Appropriate Timeouts: Configure timeouts based on your upstream service characteristics to prevent hanging requests.
- Error Handling: Implement custom error handlers for better error reporting and monitoring.
-
Security: Be cautious with
ModifyRequestto avoid exposing sensitive headers to untrusted upstreams. -
Path Rewriting: Use the
Rewritefunction for API versioning or routing transformations. - Load Balancing: For production load balancing, consider implementing health checks and weighted distributions.