Reverse Proxy
A reverse proxy is a server architecture that receives requests from clients and forwards them to one or more backend servers. Unlike a forward proxy (which represents clients), a reverse proxy operates on behalf of the server side.
Key advantages of reverse proxies:
- Load Balancing: Distributes requests across multiple servers
- Enhanced Security: Conceals real server information
- Content Caching: Improves performance
- Path Rewriting and Forwarding: Flexibly routes requests
The Salvo framework provides middleware with reverse proxy functionality.
Example Code
use salvo::prelude::*;
use salvo::proxy::Proxy;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();
let router = Router::new()
.push(
Router::new()
.path("google/{**rest}")
.handle(Proxy::<Vec<&str>>::new(vec!["https://www.google.com"])),
)
.push(
Router::new()
.path("baidu/{**rest}")
.handle(Proxy::<Vec<&str>>::new(vec!["https://www.baidu.com"])),
);
let acceptor = TcpListener::new("127.0.0.1:5800").bind().await; Server::new(acceptor).serve(router).await;
}