# Seeking help in \`Application guards and virtual hosting\`

**URL:** https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217
**Category:** help
**Created:** [September 26, 2025, 4:05pm UTC](https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217 "2025-09-26T16:05:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![GHexxarBrdv](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/ghexxarbrdv/32/51292_2.png) [@GHexxarBrdv](https://users.rust-lang.org/u/GHexxarBrdv)
#### Post date: [September 26, 2025, 4:05pm UTC](https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217/1 "2025-09-26T16:05:31Z")

</div>

Hey, 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?

```rust
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.

---

<div class="post-metadata">

### Author: ![mryun](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mryun/32/51932_2.png) [@mryun](https://users.rust-lang.org/u/mryun)
#### Post date: [September 28, 2025, 4:50pm UTC](https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217/2 "2025-09-28T16:50:21Z")

</div>

Guards are function or object-oriented conditions that prevent the service from being handled. E.g. `guard::Host(...)` checks for a specific hostname (like `api.example.com`) against the request's target host. There is also `guard::Any(...)` where you can chain `.or()`.

Virtual hosting is about that, using host guards for conditionally applying services.

---

<div class="post-metadata">

### Author: ![GHexxarBrdv](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/ghexxarbrdv/32/51292_2.png) [@GHexxarBrdv](https://users.rust-lang.org/u/GHexxarBrdv)
#### Post date: [September 28, 2025, 7:22pm UTC](https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217/3 "2025-09-28T19:22:57Z")

</div>

Tat means `guard::Host` will only allow the request for specified hostname?

---

<div class="post-metadata">

### Author: ![mryun](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mryun/32/51932_2.png) [@mryun](https://users.rust-lang.org/u/mryun)
#### Post date: [September 28, 2025, 7:31pm UTC](https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217/4 "2025-09-28T19:31:00Z")

</div>

~~The request may have been done from a `x.y.z` domain, so yeah, it constrains the service to run only under `x.y.z`.~~

> **[Host in actix\_web::guard - Rust](https://docs.rs/actix-web/latest/actix_web/guard/fn.Host.html)**
>
> Creates a guard that matches requests targeting a specific host.

Beware of the www domain prefix though.

---

<div class="post-metadata">

### Author: ![mryun](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mryun/32/51932_2.png) [@mryun](https://users.rust-lang.org/u/mryun)
#### Post date: [September 28, 2025, 7:37pm UTC](https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217/5 "2025-09-28T19:37:56Z")

</div>

Sorry, I was slightly wrong.

> **[Host header - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Host)**
>
> The HTTP Host request header specifies the host and port number of the server to which the request is being sent.

Actually HTTP requests have a `Host` header, which means "the domain we're sending the request into". It may not be always present, though.

---

<div class="post-metadata">

### Author: ![mryun](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mryun/32/51932_2.png) [@mryun](https://users.rust-lang.org/u/mryun)
#### Post date: [September 28, 2025, 7:45pm UTC](https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217/6 "2025-09-28T19:45:44Z")

</div>

You can think of the host guards more of as a reflexive feature.

---

<div class="post-metadata">

### Author: ![GHexxarBrdv](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/ghexxarbrdv/32/51292_2.png) [@GHexxarBrdv](https://users.rust-lang.org/u/GHexxarBrdv)
#### Post date: [September 29, 2025, 4:58am UTC](https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217/7 "2025-09-29T04:58:20Z")

</div>

Thanks, i will look into resource you have sent.

---

<div class="post-metadata">

### Author: ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)
#### Post date: [December 28, 2025, 4:58am UTC](https://users.rust-lang.org/t/seeking-help-in-application-guards-and-virtual-hosting/134217/8 "2025-12-28T04:58:25Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
