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.

Proxy() vs full_proxy()

The difference between proxy() and full_proxy() is that if the target server fails to return a proper error, the proxy() function will return an error (in the form of a ProxyError) while the full_proxy() will return a Response with a status code indicating an error.

The full_proxy() function will only return an error if the body was already extracted from the request before it was called. Since this indicates a logic error in the code, it is a good idea to unwrap() the Result returned by full_proxy().

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()
    };

    proxy::full_proxy(request, config).unwrap()
}

Structs

ProxyConfig

Configuration for the reverse proxy.

Enums

FullProxyError

Error that can happen when calling full_proxy.

ProxyError

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

Functions

full_proxy

Sends the request to another HTTP server using the configuration.

proxy

Sends the request to another HTTP server using the configuration.