Source: https://h2corn.monicz.dev/guide/proxy/

# Behind a reverse proxy

A reverse proxy handles browser-facing TLS, ALPN negotiation, and public-edge controls. `h2corn` runs the application side of the connection on `h2c` — cleartext HTTP/2 over TCP or a Unix socket inside the trust boundary, established by [prior knowledge](https://www.rfc-editor.org/rfc/rfc9113.html#section-3.3) rather than an `Upgrade` handshake.

```
flowchart LR
  accTitle: h2corn reverse proxy protocol and trust topology
  accDescr: A client reaches a TLS-terminating reverse proxy over HTTPS. The proxy reaches h2corn over cleartext HTTP/2 prior knowledge inside a private trust boundary. Only the proxy peer is allowed to supply forwarding identity headers.

  client["client / browser<br>public network"]
  subgraph boundary["private trust boundary"]
    direction LR
    proxy["reverse proxy<br>TLS edge / ALPN"]
    app["h2corn<br>ASGI application"]
    proxy -- "h2c" --> app
  end
  client -- "HTTPS" --> proxy
```

The edge terminates client TLS. The private hop uses h2c, and forwarding identity is trusted only from an allowlisted proxy peer.

The h2c hop avoids the HTTP/1.1 *downgrade* surface associated with [request smuggling](https://portswigger.net/web-security/request-smuggling) and [HTTP/2 downgrading](https://portswigger.net/web-security/request-smuggling/advanced/http2-downgrading). A proxy with h2c upstream support keeps this boundary on HTTP/2. The edge still requires TLS, the cleartext hop requires peer isolation, and the application remains responsible for request validation.

A separate proxy is optional: `h2corn` can terminate TLS itself; see [Direct TLS](https://h2corn.monicz.dev/guide/tls/#direct-tls).

## Compatibility boundaries

| Proxy | Required capability | Local syntax check |
| --- | --- | --- |
| nginx | 1.29.4+ HTTP/2 upstream support and `ngx_http_v2_module` | `nginx -V`, `nginx -t` |
| Caddy | `reverse_proxy` with h2c transport | `caddy validate` |
| HAProxy | 2.4+ for WebSockets over HTTP/2, `proto h2`, and PROXY protocol v2 | `haproxy -c` |

## Proxy headers and PROXY protocol

`h2corn` accepts two kinds of proxy metadata, both opt-in and restricted by [`--trusted-peers`](https://h2corn.monicz.dev/configuration/#option-trusted_peers):

- **[`--proxy-headers`](https://h2corn.monicz.dev/configuration/#option-proxy_headers)** enables standard `Forwarded` and `X-Forwarded-*` processing for peers in `--trusted-peers`. By default it interprets `X-Forwarded-For` and `X-Forwarded-Proto`. Use [`--forwarded-fields`](https://h2corn.monicz.dev/configuration/#option-forwarded_fields) `for,proto,host,port,prefix` to select additional `X-Forwarded-*` facts, or `--forwarded-fields forwarded` to select the [RFC 7239](https://datatracker.ietf.org/doc/html/rfc7239) dialect. The two dialects cannot be combined.
- **[`--proxy-protocol`](https://h2corn.monicz.dev/configuration/#option-proxy_protocol) `v1|v2`** parses HAProxy's [PROXY protocol](https://github.com/haproxy/haproxy/blob/master/doc/proxy-protocol.txt) on inbound connections. It carries transport-level peer information on the connection itself for original source IP metrics or per-IP limits.

Proxy headers carry request metadata. Add PROXY protocol when the upstream is explicitly configured to send it.

`h2corn` reads a forwarding header from the right, skipping hops that match `--trusted-peers` and taking the first address outside that set. Client- prepended values sit to the left of the proxy values and are ignored. A proxy that *appends* rather than replaces — nginx's `proxy_add_x_forwarded_for`, HAProxy's `option forwardfor`, Caddy's default — matches the parser's expected append order.

nginx and Caddy emit `X-Forwarded-*` natively; HAProxy can send `Forwarded` with `option forwarded`. The selected fields are applied only for a peer in `--trusted-peers`. With `--proxy-headers` enabled, h2corn removes every recognized forwarding header that is not selected or does not come from a trusted peer before building `scope["headers"]`. Selected, consumed headers remain visible to the application.

Removal includes underscore spellings such as `X_Forwarded_For`. Applications that read forwarding headers directly, including [Django](https://www.djangoproject.com/) with `USE_X_FORWARDED_HOST`, see only the selected headers from trusted peers.

[`--proxy-headers`](https://h2corn.monicz.dev/configuration/#option-proxy_headers) with an empty `--forwarded-fields`, or an empty [`--trusted-peers`](https://h2corn.monicz.dev/configuration/#option-trusted_peers), is rejected at startup.

## nginx

[nginx](https://nginx.org/) supports HTTP/2 upstream from **1.29.4**. Set [`proxy_http_version 2`](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version) and keep the upstream URL on `http://`, which makes that hop cleartext `h2c`. The deployed build must include `ngx_http_v2_module`.

The [NGINX 1.29.4 announcement](https://blog.nginx.org/blog/nginx-open-source-1-29-3-and-1-29-4) notes that its initial HTTP/2 upstream implementation does not multiplex requests: one upstream connection handles one request at a time. Verify connection reuse and concurrency on the deployed build; the `h2c` hop provides HTTP/2 framing, while the browser-facing hop multiplexes normally.

WebSockets need their own HTTP/1.1 location

nginx performs the HTTP/1.1 `Upgrade` handshake and does not translate it into the [RFC 8441](https://datatracker.ietf.org/doc/html/rfc8441) extended `CONNECT` that HTTP/2 requires (see [WebSockets](https://h2corn.monicz.dev/guide/websockets/#http2-only-deployment)). An HTTP/2 upstream cannot carry this handshake. Route `/ws` over HTTP/1.1 and all other requests over `h2c`. HTTP/1 must remain enabled when nginx serves WebSockets. For WebSockets over a pure HTTP/2 upstream, use [HAProxy](https://h2corn.monicz.dev/guide/proxy/#haproxy).

nginx.conf (configuration template)

```nginx
# HTTP/2 to the upstream needs nginx built with ngx_http_v2_module. The feature
# appeared in 1.29.4, and the module has carried security advisories since, so
# run a supported release and check nginx's advisory list against it.
# `proxy_pass http://` keeps the upstream hop cleartext h2c, so there is no
# second TLS handshake between proxy and application.

upstream h2corn_backend {
    server 127.0.0.1:8000;

    # Without this every request opens a fresh upstream connection, which is
    # the cost h2c is supposed to remove.
    keepalive 32;
}

server {
    listen 443 ssl;
    http2 on;

    server_name example.com;
    ssl_certificate     /etc/nginx/certs/example.crt;
    ssl_certificate_key /etc/nginx/certs/example.key;

    # WebSockets first: nginx still speaks the HTTP/1.1 Upgrade handshake and
    # does not translate it into the RFC 8441 extended CONNECT that HTTP/2
    # requires, so upgrade traffic must take its own HTTP/1.1 location. This
    # nginx topology requires h2corn HTTP/1 to remain enabled.
    location /ws {
        proxy_pass http://h2corn_backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade    $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header Host              $host;
        proxy_set_header X-Forwarded-For   $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host  $host;
        proxy_set_header X-Forwarded-Port  $server_port;
        # h2corn consumes only its configured forwarding fields and strips
        # other recognized forwarding headers before they reach the app.
    }

    location / {
        proxy_pass http://h2corn_backend;
        proxy_http_version 2;

        # Without this nginx sends the upstream *group* name — here
        # `h2corn_backend` — as Host/`:authority`, which is what host-based
        # routing and TrustedHostMiddleware would then see.
        proxy_set_header Host $host;

        # h2corn consumes only its configured forwarding fields and strips the
        # other recognized forwarding headers before they reach the app.
        proxy_set_header X-Forwarded-For   $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host  $host;
        proxy_set_header X-Forwarded-Port  $server_port;
    }
}
```

Run h2corn with the proxy-facing settings:

```bash
h2corn hello:app \
  --bind 127.0.0.1:8000 \
  --proxy-headers \
  --forwarded-fields for,proto,host,port \
  --trusted-peers 127.0.0.1,::1
```

## Caddy

[Caddy](https://caddyserver.com/) documents native `h2c` upstream support with its [`reverse_proxy`](https://caddyserver.com/docs/caddyfile/directives/reverse_proxy) directive.

Caddy WebSockets

This example routes HTTP/1.1 requests carrying `Upgrade` to an HTTP/1.1 upstream and other requests to `h2c`. Keep h2corn HTTP/1 enabled for that WebSocket route.

Caddyfile (configuration template)

```caddy
example.com {
    # This split route sends WebSocket upgrades over HTTP/1.1; keep h2corn
    # HTTP/1 enabled. Use --no-http1 only with a separately verified pure h2c
    # route whose proxy translates upgrades to RFC 8441 extended CONNECT.
    @websockets {
        header Connection *Upgrade*
        header Upgrade websocket
    }
    reverse_proxy @websockets 127.0.0.1:8000 {
        # h2corn strips recognized forwarding headers it does not consume.
    }

    reverse_proxy h2c://127.0.0.1:8000 {
        # h2corn strips recognized forwarding headers it does not consume.
    }
}
```

Run h2corn with the same proxy-facing flags as nginx above (adding `unix` to `--trusted-peers` when proxying over a Unix socket).

## HAProxy

[HAProxy](https://www.haproxy.com/) speaks HTTP/2 upstream with `proto h2` and can layer PROXY protocol v2 on the same connection — see the [HAProxy HTTP guide](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/protocol-support/http/) for the full directive set.

HAProxy 2.4 and newer supports WebSockets over HTTP/2 using RFC 8441 extended `CONNECT` ([release notes](https://www.haproxy.com/blog/announcing-haproxy-2-4)). This example requires RFC 8441 clients. For HTTP/1.1 WebSocket clients, use a separate HTTP/1.1 upstream route and keep h2corn HTTP/1 enabled. [`--no-http1`](https://h2corn.monicz.dev/configuration/#option-http1) applies when every route uses HTTP/2 and the WebSocket client/proxy path supports RFC 8441.

haproxy.cfg (configuration template)

```text
global
    log stdout format raw daemon

defaults
    mode http
    timeout connect 5s
    timeout client 30s
    timeout server 30s
    # An idle WebSocket has to outlive the keepalive that proves it is alive:
    # h2corn's first ping goes out at websocket_ping_interval (60s by default),
    # so a 30s idle timeout would close the connection before it ever pinged.
    timeout tunnel 300s

frontend public_https
    bind :443 ssl crt /etc/haproxy/certs/example.pem alpn h2,http/1.1

    # h2corn consumes only its configured forwarding fields and strips the
    # other recognized forwarding headers before they reach the app.
    http-request set-header X-Forwarded-For %[src]
    http-request set-header X-Forwarded-Proto %[ssl_fc,iif(https,http)]
    http-request set-header X-Forwarded-Host %[req.hdr(host)]
    http-request set-header X-Forwarded-Port %[dst_port]

    default_backend h2corn_backend

backend h2corn_backend
    server app1 127.0.0.1:8000 check proto h2 send-proxy-v2
```

Run h2corn with the proxy-facing settings:

```bash
h2corn hello:app \
  --bind 127.0.0.1:8000 \
  --proxy-protocol v2 \
  --proxy-headers \
  --forwarded-fields for,proto,host,port \
  --trusted-peers 127.0.0.1,::1,unix \
  --no-http1
```

## Other proxies

Other proxies need the same h2c, forwarding-header, and WebSocket checks. If a proxy cannot speak h2c, HTTP/1.1 remains the alternative private-hop protocol and [`--no-http1`](https://h2corn.monicz.dev/configuration/#option-http1) must be omitted.
