Proxy Inverso

Un proxy inverso è un'architettura server che riceve richieste dai client e le inoltra a uno o più server backend. A differenza di un proxy diretto (che agisce per conto dei client), un proxy inverso opera per conto del lato server.

Vantaggi principali dei proxy inversi:

  • Bilanciamento del carico: Distribuisce le richieste su più server
  • Sicurezza rafforzata: Nasconde le informazioni del server reale
  • Memorizzazione nella cache dei contenuti: Migliora le prestazioni
  • Riscrittura e inoltro dei percorsi: Instrada le richieste in modo flessibile

Il framework Salvo fornisce middleware per la funzionalità di proxy inverso.

Codice di Esempio

main.rs
Cargo.toml
use salvo::prelude::*;

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt().init();

    // In this example, if the requested URL begins with <http://127.0.0.1:8698/>, the proxy goes to
    // <https://www.rust-lang.org>; if the requested URL begins with <http://localhost:8698/>, the proxy
    // goes to <https://crates.io>.
    let router = Router::new()
        .push(
            Router::new()
                .host("127.0.0.1")
                .path("{**rest}")
                .goal(Proxy::use_hyper_client("https://docs.rs")),
        )
        .push(
            Router::new()
                .host("localhost")
                .path("{**rest}")
                .goal(Proxy::use_hyper_client("https://crates.io")),
        );

    let acceptor = TcpListener::new("0.0.0.0:8698").bind().await;
    Server::new(acceptor).serve(router).await;
}