This example forwards requests to a single HTTP upstream with
ntex::client::Client. The forward handler in src/main.rs preserves the
request path and query, copies headers, and adds X-Forwarded-For when a peer
address is available. Request and response bodies are buffered, not streamed.
A reachable HTTP upstream server is required. The example below assumes one
is already listening on 127.0.0.1:3000.
cd extras/http-proxy
cargo run -- 127.0.0.1 8080 127.0.0.1 3000With these arguments, the proxy listens on 127.0.0.1:8080 and forwards to
http://127.0.0.1:3000. The four required positional arguments, in order,
are listen_addr, listen_port, forward_addr, and forward_port. Keep
Cargo's -- separator before them.
curl -i 'http://127.0.0.1:8080/example?name=ntex'This forwards the request to http://127.0.0.1:3000/example?name=ntex and
returns the upstream status and body. The upstream decides the response.
cargo test -p http-proxyCovers request and response forwarding and upstream error responses using local test servers.
- Handlers and application state
- HTTP clients, including HTTPS upstreams
- Test servers