How to handle subdomains #3103
-
SummaryI want to handle
Store where? "Captured at key" - what does that even mean?
Any why does wildcard not handle The only discussion I found regarding axum and subdomains had a non working solution, not to mention "use reverse proxy" "use nginx" "dont handle that with axum" "use loadbalancer" "dont do it like this" I do not want to do any of that. I just want to handle subdomains in rust code directly. Any help besides "use nginx" is appreciated. If this sounds like a rant, it is probably because I come from a nodejs + express background. axum version=0.7.9 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Route works with part of the url, which is located after the host. Applications cannot manage their DNS names themselves. However, several names can refer to one instance. And this instance is able to distinguish from which Hostname request came If you want two third level domains to refer to one instance of the axum application, then this is done outside the Axum. Before. It doesn’t matter through Nginx, or through something else. You need to ensure that the right header HOST reaches the application. And then inside the axum application get from the “request” Host header and branch the subsequent logic as you wish. |
Beta Was this translation helpful? Give feedback.
-
I managed to solve it.
New code (I did have to add the let api_router: Router = Router::new().route("/", get(api)).fallback(fof);
let all_router: Router = Router::new()
.route("/", get(|| async { "Coming soon!" }))
.fallback(fof);
let app: Router = Router::new()
.route("/", get(root))
.route("/api", get(api))
.fallback(fof);
let subdomain_handler = |Host(h), req: Request| async move {
println!("{h}");
match h.as_str() {
h if h == format!("api.{}", *DOMAIN) => api_router.oneshot(req).await,
h if h == format!("all.{}", *DOMAIN) => all_router.oneshot(req).await,
_ => app.oneshot(req).await,
}
};
let main = axum::serve(
TcpListener::bind(format!("0.0.0.0:{}", *PORT))
.await
.unwrap(),
subdomain_handler.into_make_service(),
); |
Beta Was this translation helpful? Give feedback.
I managed to solve it.
wildcard
in issuesRouter.fallback()
based on #1607 (comment)New code (I did have to add the
Router
andaxum::extract::Request
types):let ap…