|
1 | 1 | # Rust Reverse Proxy |
2 | 2 |
|
3 | | -[](https://github.com/HueCodes/rust-reverse-proxy/actions/workflows/rust.yml) |
4 | | -[](https://crates.io/crates/rust-reverse-proxy) |
5 | | - |
6 | | -A lightweight and fast HTTP reverse proxy written in Rust. This project leverages the [Warp](https://github.com/seanmonstar/warp) framework for efficient request handling and routing, providing a simple yet powerful solution for proxying HTTP traffic. |
| 3 | +A lightweight, high-performance HTTP reverse proxy written in Rust, prioritizing security, memory safety, and concurrency. |
7 | 4 |
|
8 | 5 | ## Features |
9 | 6 |
|
10 | | -- **High Performance**: Built with Rust's zero-cost abstractions and async runtime for low-latency proxying. |
11 | | -- **Flexible Routing**: Supports multiple upstream backends with easy configuration. |
12 | | -- **TLS Support**: Optional TLS termination and forwarding (planned). |
13 | | -- **Logging and Metrics**: Built-in structured logging and basic metrics (in development). |
14 | | -- **Configurable**: YAML or TOML-based configuration for backends, filters, and middleware. |
| 7 | +- **High Performance**: Built with Tokio and Hyper for maximum throughput |
| 8 | +- **Load Balancing**: Round-robin load balancing across multiple backend servers |
| 9 | +- **Health Checks**: Automatic health monitoring of backend servers |
| 10 | +- **Configuration-Driven**: YAML-based configuration for easy management |
| 11 | +- **Structured Logging**: Comprehensive request/response logging with configurable levels |
| 12 | +- **Fault Tolerance**: Automatic failover and error handling |
| 13 | +- **Request Timeouts**: Configurable timeouts for reliability |
| 14 | +- **Header Forwarding**: Proper X-Forwarded-* headers for backend services |
15 | 15 |
|
16 | 16 | ## Quick Start |
17 | 17 |
|
18 | | -### Prerequisites |
19 | | - |
20 | | -- Rust 1.70+ (stable) |
21 | | -- Cargo |
22 | | - |
23 | | -### Installation |
24 | | - |
25 | | -Clone the repository and build with Cargo: |
26 | | - |
27 | | -```bash |
28 | | -git clone https://github.com/HueCodes/rust-reverse-proxy.git |
29 | | -cd rust-reverse-proxy |
30 | | -cargo build --release |
31 | | -``` |
| 18 | +1. **Build the project**: |
| 19 | + ```bash |
| 20 | + cargo build --release |
| 21 | + ``` |
| 22 | + |
| 23 | +2. **Configure your backends** in `config.yaml`: |
| 24 | + ```yaml |
| 25 | + server: |
| 26 | + host: "127.0.0.1" |
| 27 | + port: 3000 |
| 28 | + |
| 29 | + backends: |
| 30 | + - url: "http://backend1.example.com" |
| 31 | + weight: 1 |
| 32 | + health_check_path: "/health" |
| 33 | + - url: "http://backend2.example.com" |
| 34 | + weight: 1 |
| 35 | + health_check_path: "/health" |
| 36 | + ``` |
| 37 | +
|
| 38 | +3. **Run the proxy**: |
| 39 | + ```bash |
| 40 | + cargo run --release |
| 41 | + ``` |
32 | 42 |
|
33 | | -### Running the Proxy |
| 43 | +## Configuration |
34 | 44 |
|
35 | | -Create a basic configuration file `config.yaml`: |
| 45 | +The proxy is configured using a `config.yaml` file: |
36 | 46 |
|
37 | 47 | ```yaml |
38 | | -backends: |
39 | | - - name: "api" |
40 | | - host: "http://localhost:8080" |
41 | | - path_prefix: "/api" |
42 | | - - name: "web" |
43 | | - host: "http://localhost:3000" |
44 | | - path_prefix: "/" |
45 | | - |
46 | | -port: 80 |
47 | | -``` |
| 48 | +server: |
| 49 | + host: "127.0.0.1" # Interface to bind to |
| 50 | + port: 3000 # Port to listen on |
48 | 51 |
|
49 | | -Start the proxy: |
50 | | -
|
51 | | -```bash |
52 | | -cargo run -- --config config.yaml |
| 52 | +backends: |
| 53 | + - url: "http://httpbin.org" # Backend server URL |
| 54 | + weight: 1 # Load balancing weight |
| 55 | + health_check_path: "/status/200" # Health check endpoint |
| 56 | + - url: "http://example.com" |
| 57 | + weight: 1 |
| 58 | + health_check_path: "/" |
| 59 | + |
| 60 | +load_balancing: |
| 61 | + strategy: "round_robin" # Load balancing strategy |
| 62 | + |
| 63 | +health_checks: |
| 64 | + enabled: true # Enable/disable health checks |
| 65 | + interval_seconds: 30 # Health check interval |
| 66 | + timeout_seconds: 5 # Health check timeout |
| 67 | + failure_threshold: 3 # Failures before marking unhealthy |
| 68 | + |
| 69 | +logging: |
| 70 | + level: "info" # Log level (debug, info, warn, error) |
| 71 | + format: "json" # Log format (json, text) |
| 72 | + |
| 73 | +timeouts: |
| 74 | + request_timeout_seconds: 30 # Backend request timeout |
| 75 | + connect_timeout_seconds: 10 # Connection timeout |
53 | 76 | ``` |
54 | 77 |
|
55 | | -Or with the release binary: |
| 78 | +## Architecture |
56 | 79 |
|
57 | | -```bash |
58 | | -./target/release/rust-reverse-proxy --config config.yaml |
59 | | -``` |
| 80 | +The proxy consists of several key components: |
60 | 81 |
|
61 | | -The proxy will listen on port 80 and forward requests to the configured backends based on path prefixes. |
| 82 | +- **ProxyService**: Main request handling service |
| 83 | +- **LoadBalancer**: Manages backend selection and health status |
| 84 | +- **HealthChecker**: Monitors backend server health |
| 85 | +- **Configuration**: YAML-based configuration management |
62 | 86 |
|
63 | | -## Configuration |
| 87 | +## Load Balancing |
64 | 88 |
|
65 | | -The proxy uses a YAML configuration file. Key sections: |
| 89 | +Currently supports round-robin load balancing: |
| 90 | +- Requests are distributed evenly across healthy backends |
| 91 | +- Unhealthy backends are automatically excluded |
| 92 | +- Backends are marked unhealthy after consecutive failures |
66 | 93 |
|
67 | | -- `backends`: List of upstream services with `name`, `host`, `path_prefix`, and optional `strip_prefix`. |
68 | | -- `port`: Listening port (default: 80). |
69 | | -- `log_level`: Logging verbosity (e.g., "info", "debug"). |
70 | | -- `tls`: TLS configuration (cert and key paths, enabled by default if provided). |
| 94 | +## Health Checks |
71 | 95 |
|
72 | | -For full options, see `config.yaml.example` (to be added). |
| 96 | +- Periodic health checks to all configured backends |
| 97 | +- Configurable health check paths and intervals |
| 98 | +- Automatic failover when backends become unhealthy |
| 99 | +- Recovery detection when backends come back online |
73 | 100 |
|
74 | | -## Usage Examples |
| 101 | +## Logging |
75 | 102 |
|
76 | | -### Basic Proxying |
| 103 | +Structured logging with configurable levels: |
| 104 | +- Request/response logging with timing information |
| 105 | +- Health check status updates |
| 106 | +- Error tracking and debugging information |
| 107 | +- JSON or plain text output formats |
77 | 108 |
|
78 | | -Proxy all `/api/*` requests to `http://backend:8080`: |
| 109 | +## Error Handling |
79 | 110 |
|
80 | | -```yaml |
81 | | -backends: |
82 | | - - name: "backend" |
83 | | - host: "http://backend:8080" |
84 | | - path_prefix: "/api" |
85 | | -``` |
| 111 | +Robust error handling with appropriate HTTP status codes: |
| 112 | +- `503 Service Unavailable`: No healthy backends |
| 113 | +- `502 Bad Gateway`: Backend request failures |
| 114 | +- `504 Gateway Timeout`: Backend request timeouts |
| 115 | +- `400 Bad Request`: Client request issues |
86 | 116 |
|
87 | | -### Load Balancing |
| 117 | +## Performance |
88 | 118 |
|
89 | | -Support for round-robin load balancing across multiple hosts (planned feature): |
90 | | -
|
91 | | -```yaml |
92 | | -backends: |
93 | | - - name: "api-cluster" |
94 | | - hosts: |
95 | | - - "http://backend1:8080" |
96 | | - - "http://backend2:8080" |
97 | | - path_prefix: "/api" |
98 | | - strategy: "round_robin" |
99 | | -``` |
100 | | -
|
101 | | -### Middleware |
102 | | -
|
103 | | -Add rate limiting or authentication middleware via configuration (in development). |
| 119 | +Optimized for high performance: |
| 120 | +- Async/await with Tokio runtime |
| 121 | +- Connection pooling with Hyper client |
| 122 | +- Zero-copy request/response forwarding where possible |
| 123 | +- Minimal memory allocations |
104 | 124 |
|
105 | 125 | ## Development |
106 | 126 |
|
107 | | -This project is actively under development. Current focus: |
108 | | -
|
109 | | -- Implementing core proxy logic with Warp. |
110 | | -- Adding configuration parsing. |
111 | | -- TLS integration with Rustls. |
112 | | -
|
113 | | -### Building and Testing |
| 127 | +### Building |
| 128 | +```bash |
| 129 | +cargo build |
| 130 | +``` |
114 | 131 |
|
| 132 | +### Testing |
115 | 133 | ```bash |
116 | | -# Run tests |
117 | 134 | cargo test |
118 | | - |
119 | | -# Run with clippy |
120 | | -cargo clippy -- -D warnings |
121 | | - |
122 | | -# Format code |
123 | | -cargo fmt |
124 | 135 | ``` |
125 | 136 |
|
126 | | -### Dependencies |
127 | | - |
128 | | -See `Cargo.toml` for details. Key crates: |
| 137 | +### Running with Debug Logging |
| 138 | +```bash |
| 139 | +RUST_LOG=debug cargo run |
| 140 | +``` |
129 | 141 |
|
130 | | -- `warp`: Web server framework. |
131 | | -- `tokio`: Async runtime. |
132 | | -- `serde`: Configuration serialization. |
133 | | -- `tracing`: Logging. |
| 142 | +## Docker Support |
| 143 | + |
| 144 | +Create a `Dockerfile`: |
| 145 | +```dockerfile |
| 146 | +FROM rust:1.70 AS builder |
| 147 | +WORKDIR /app |
| 148 | +COPY . . |
| 149 | +RUN cargo build --release |
| 150 | +
|
| 151 | +FROM debian:bookworm-slim |
| 152 | +RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/* |
| 153 | +COPY --from=builder /app/target/release/reverse_proxy /usr/local/bin/ |
| 154 | +COPY config.yaml /etc/reverse-proxy/ |
| 155 | +WORKDIR /etc/reverse-proxy |
| 156 | +EXPOSE 3000 |
| 157 | +CMD ["reverse_proxy"] |
| 158 | +``` |
134 | 159 |
|
135 | 160 | ## Contributing |
136 | 161 |
|
137 | | -Contributions are welcome! Please: |
138 | | - |
139 | | -1. Fork the repository. |
140 | | -2. Create a feature branch (`git checkout -b feature/my-feature`). |
141 | | -3. Commit changes (`git commit -am 'Add my feature'`). |
142 | | -4. Push to the branch (`git push origin feature/my-feature`). |
143 | | -5. Open a Pull Request. |
144 | | - |
145 | | -See [CONTRIBUTING.md](CONTRIBUTING.md) for more details (to be created). |
| 162 | +1. Fork the repository |
| 163 | +2. Create a feature branch |
| 164 | +3. Make your changes |
| 165 | +4. Add tests if applicable |
| 166 | +5. Submit a pull request |
146 | 167 |
|
147 | 168 | ## License |
148 | 169 |
|
149 | | -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
150 | | - |
151 | | -## Acknowledgments |
152 | | - |
153 | | -- Inspired by various Rust web projects and the need for a simple, performant proxy. |
154 | | -- Thanks to the Warp and Tokio teams for excellent crates. |
| 170 | +This project is licensed under the MIT License - see the LICENSE file for details. |
155 | 171 |
|
156 | | ---- |
| 172 | +## Roadmap |
157 | 173 |
|
158 | | -*Project started in 2025. More features coming soon!* |
| 174 | +- [ ] HTTPS/TLS termination support |
| 175 | +- [ ] WebSocket proxying |
| 176 | +- [ ] Metrics and monitoring endpoints |
| 177 | +- [ ] Rate limiting |
| 178 | +- [ ] Path-based routing |
| 179 | +- [ ] Circuit breaker pattern |
| 180 | +- [ ] Admin API for runtime configuration |
0 commit comments