Module rouille::proxy [] [src]

Dispatch a request to another HTTP server.

This module provides functionnalities to dispatch a request to another server. This can be used to make rouille behave as a reverse proxy.

This function call will return immediately after the remote server has finished sending its headers. The socket to the remote will be stored in the ResponseBody of the response.

Example

You can for example dispatch to a different server depending on the host requested by the client.

use rouille::{Request, Response};
use rouille::proxy;

fn handle_request(request: &Request) -> Response {
    let config = match request.header("Host") {
        Some(h) if h == "domain1.com" => {
            proxy::ProxyConfig {
                addr: "domain1.handler.localnetwork",
                replace_host: None,
            }
        },

        Some(h) if h == "domain2.com" => {
            proxy::ProxyConfig {
                addr: "domain2.handler.localnetwork",
                replace_host: None,
            }
        },

        _ => return Response::empty_404()
    };

    match proxy::proxy(request, config) {
        Ok(r) => r,
        Err(_) => Response::text("Bad gateway").with_status_code(500),
    }
}

Structs

ProxyConfig

Configuration for the reverse proxy.

Enums

ProxyError

Error that can happen when dispatching the request to another server.

Functions

proxy

Sends the request to another HTTP server using the configuration.