Skip to main content

How to retrieve host

A CDN resource can be configured with several defined hostnames. You may want to retrieve the chosen host for various purposes.

To configure alternate domains, add more domains on custom domains section

Here is an example to retrieve and log this data.

Use this solution in your CDB Edge Compute service:

use proxy_wasm::traits::*;
use proxy_wasm::types::*;

proxy_wasm::main! {{
    proxy_wasm::set_log_level(LogLevel::Trace);
    proxy_wasm::set_root_context(|_| Box::new(HostRedirector {}));
}}

struct HostRedirector;

impl Context for HostRedirector {}

impl HttpContext for HostRedirector {
    fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
        // Log the header X-CDN-Real-Host
        let real_host = self.get_http_request_header("X-CDN-Real-Host").unwrap_or_default();
        println!("X-CDN-Real-Host: {}", real_host);

        Action::Continue
    }
}

[package]
name = "host-redirector"
version = "0.1.0"
edition = "2024"
description = "Proxy-Wasm example: Getting real Host from headers"

[lib]
crate-type = ["cdylib"]

[dependencies]
fastedge = "0.2.0"
proxy-wasm = "0.2.1"
headers = "0.4.0"