Seeking help in `Application guards and virtual hosting`
⚓ Rust 📅 2025-09-26 👤 surdeus 👁️ 7Hey, i am just exploring the actix web for building web servers in rust. While reading docs i got to know about guards and virtual hosting. But i am not getting it, Can anyone help me to understand the actual use of it and why they are used and when should we use them?
You can think of a guard as a simple function that accepts a request object reference and returns true or false. Formally, a guard is any object that implements the Guard trait. Actix Web provides several guards. You can check the functions section of the API docs.
One of the provided guards is Host. It can be used as a filter based on request header information.
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(
                web::scope("/")
                    .guard(guard::Host("www.rust-lang.org"))
                    .route("", web::to(|| async { HttpResponse::Ok().body("www") })),
            )
            .service(
                web::scope("/")
                    .guard(guard::Host("users.rust-lang.org"))
                    .route("", web::to(|| async { HttpResponse::Ok().body("user") })),
            )
            .route("/", web::to(HttpResponse::Ok))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}
Above is the content from the docs.
1 post - 1 participant
🏷️ Rust_feed