Nyx con Ejemplos

Hosts virtuales del proxy

El ruteo por host virtual mapea el header HTTP Host (o el SNI de TLS para HTTPS) a distintos upstreams. Así es como nyx-proxy sirve 4 dominios en el puerto :443 simultáneamente.

Código

// nyx-proxy virtual host routing — match Host header to upstream
// Routing nyx-proxy — mapear Host header a upstream

fn route_for_host(host: String) -> String {
    // Map each domain to an upstream IP:port
    if host == "nyxlang.com" { return "127.0.0.1:3001" }
    if host == "nyxkv.com" { return "127.0.0.1:3002" }
    if host == "serve.nyxlang.com" { return "127.0.0.1:3003" }
    if host == "proxy.nyxlang.com" { return "127.0.0.1:3005" }
    // Default upstream
    return "127.0.0.1:3001"
}

fn parse_host_header(request: String) -> String {
    // Find "Host: " in the request headers
    let idx: int = request.indexOf("Host: ")
    if idx < 0 { return "" }
    let start: int = idx + 6
    let end: int = request.indexOf("\r\n", start)
    if end < 0 { return "" }
    return request.substring(start, end)
}

fn main() -> int {
    // Simulate reading an HTTP request
    let request: String = "GET / HTTP/1.1\r\nHost: nyxkv.com\r\nUser-Agent: test\r\n\r\n"

    let host: String = parse_host_header(request)
    let upstream: String = route_for_host(host)

    print("incoming Host: " + host)
    print("route to -> " + upstream)

    // In production, nyx-proxy tls_accept() reads SNI, matches the cert,
    // then Host header matches the vhost config, forwards the request
    // via tcp_connect to the upstream, and pipes bytes bidirectionally.
    return 0
}

Salida

incoming Host: nyxkv.com
route to -> 127.0.0.1:3002

Explicación

El virtual hosting es cómo una sola IP de servidor aloja muchos sitios web. Para HTTP plano, el header Host dentro del request indica qué sitio quiere el cliente; para HTTPS, la extensión SNI en el ClientHello de TLS hace lo mismo antes de que fluya cualquier HTTP. nyx-proxy usa ambos por capas: SNI para elegir el certificado correcto en el handshake de TLS, y luego el header Host para elegir el upstream correcto en la capa 7. Acá route_for_host es una búsqueda plana, pero la configuración real carga esta tabla desde proxy.toml para que operaciones pueda agregar dominios sin recompilar. Este mismo patrón sirve los cuatro dominios de la familia nyxlang.com desde un único puerto :443.

← Anterior Siguiente →

Source: examples/by-example/86-proxy-vhost.nx