xin

Cloudflare Tunnel

xin can connect to Cloudflare as a tunnel connector itself. Visitors reach your sites through Cloudflare with no open inbound port, and no cloudflared process runs beside xin.

Requests that arrive through the tunnel go through the same routing as any other request: server_name selects the server, locations, limits, logging, caching and every content module behave exactly as they do on a socket listener. HTTP/1.1, WebSocket and, over the HTTP/2 transport, gRPC all work.

Availability

Cloudflare Tunnel support is included in every xin package and release binary.

Quick start

The fastest way to see it work is a quick tunnel: an anonymous trycloudflare.com hostname that needs no account.

events {}
http {
    cloudflare_tunnel demo quick;

    server {
        listen cftunnel:demo;
        location / { return 200 "hello through Cloudflare\n"; }
    }
}

Start xin and look in the error log for the line that reports the tunnel's public URL (https://….trycloudflare.com). Quick tunnels are for trying things out; Cloudflare gives them no uptime guarantee.

A real tunnel

Create a tunnel in the Cloudflare dashboard (Zero Trust, Networks, Tunnels), or with cloudflared tunnel create, then give xin its credentials.

Token from the dashboard

Copy the token the dashboard shows for the tunnel (the value after cloudflared service install) into a file readable only by xin:

http {
    cloudflare_tunnel app token_file=/etc/xinginx/cloudflare-app.token;

    server {
        listen cftunnel:app;
        server_name app.example.com;
        location / { proxy_pass http://127.0.0.1:3000; }
    }
    server {
        listen cftunnel:app;
        server_name api.example.com;
        location / { proxy_pass http://127.0.0.1:4000; }
    }
}

For containers, read the token from the environment instead: cloudflare_tunnel app token_env=TUNNEL_TOKEN;

Credentials file

If you created the tunnel with cloudflared tunnel create NAME, point xin at the <uuid>.json file it wrote:

cloudflare_tunnel app credentials_file=/etc/xinginx/11111111-2222-3333-4444-555555555555.json;

Point a hostname at the tunnel

Cloudflare must route each hostname to the tunnel. Either:

  • In the dashboard, open the tunnel, choose Public Hostname and add the hostname; or
  • create a proxied DNS CNAME from the hostname to <tunnel-uuid>.cfargotunnel.com.

The service URL the dashboard asks for is ignored. xin routes by server_name, so put whatever placeholder the dashboard accepts and configure the real behavior in xin. A hostname that no server_name matches is served by the tunnel's default_server, as on any shared port.

Directives

cloudflare_tunnel NAME CREDENTIALS [options];

Context: http. Declares a tunnel. Exactly one credential source is required:

CredentialMeaning
token_file=PATHFile holding the dashboard token. Surrounding whitespace is trimmed.
token_env=VARThe same token from an environment variable, read when the configuration loads.
credentials_file=PATHThe JSON file written by cloudflared tunnel create.
quickAn anonymous trycloudflare.com tunnel.

Options:

OptionDefaultMeaning
protocol=auto|quic|http2autoTransport to the Cloudflare edge. auto uses QUIC and falls back to HTTP/2 when UDP to the edge is blocked. Choose http2 for gRPC.
ha_connections=N4 (1 for quick)Concurrent connections to the edge, 1 to 16.
edge_ip_version=auto|4|6autoAddress family used to reach the edge.
region=NAMEnoneConnect to a regional edge such as us.
grace_period=TIME30sHow long in-flight requests may finish when a connection is retired by a reload or shutdown. Maximum 3 minutes.
real_ip=on|offonWhether $remote_addr is the visitor (see below).
edge=HOST:PORT[,…]automaticStatic edge addresses instead of DNS discovery, for restricted egress.
edge_ca=PATHbuilt inPEM roots used to verify the edge instead of the built-in set.

listen cftunnel:NAME [default_server];

Context: server. Attaches a server to a tunnel. Several servers can share a tunnel and are selected by server_name. Socket options such as ssl, http2, quic, reuseport or backlog do not apply and are rejected on a cftunnel: line. TLS is terminated by Cloudflare, so no certificates are configured in xin. A tunnel with no server, or a cftunnel: listener with no matching tunnel, is a configuration error.

Token and credentials files are read when the configuration loads. xin warns if another user can read them, never logs the secret, and never echoes a token in an error message. xinginx -t checks the credentials without contacting Cloudflare.

Client address and scheme

  • With real_ip=on (the default), $remote_addr is the visitor's address as reported by Cloudflare, and $realip_remote_addr is the edge address. This applies to tunnel traffic only and never changes how socket listeners behave. If Cloudflare sends no valid visitor address, $remote_addr stays the edge address.
  • With real_ip=off, $remote_addr is the edge address and the realip module works as usual.
  • $scheme is https when the visitor used HTTPS to Cloudflare, otherwise http; $server_port is 443 or 80 to match. Headers such as $http_cf_ray and $http_cf_connecting_ip are available as usual.

Reload and shutdown

Reloading with xinginx -s reload is make-before-break. The new configuration registers its own connections with Cloudflare first, and only then does the old one stop taking new requests. Requests already in flight finish, up to grace_period, so a reload does not drop traffic. A reload re-reads token and credentials files and the environment. A quick tunnel keeps its hostname across reloads; restarting xin creates a new one. A graceful stop retires the connections the same way.

Dashboard-managed tunnels

A tunnel created with a dashboard token is normally configured in the dashboard. With xin, the xin configuration decides routing. xin acknowledges the dashboard's ingress rules and logs which of them it is ignoring. The public hostnames still matter, because they are how DNS reaches the tunnel; the service URLs do not.

WebSocket and gRPC

WebSocket works over both transports with the usual proxy_pass upgrade headers. gRPC needs HTTP/2 trailers, which the QUIC transport cannot carry, so set protocol=http2 on a tunnel that serves gRPC:

cloudflare_tunnel grpc token_file=/etc/xinginx/grpc.token protocol=http2;

server {
    listen cftunnel:grpc;
    server_name rpc.example.com;
    location / { grpc_pass grpc://127.0.0.1:50051; }
}

Not supported

  • TCP, UDP and ICMP routing, including WARP routing and SSH or RDP through Access.
  • Protected quick tunnels (--allowed-mail).
  • Trailers over the QUIC transport (use protocol=http2 for gRPC).
  • The originRequest options and service URLs from dashboard ingress rules; routing is defined in xin.