Estimated reading time: 3 minutes
This feature allows you to handle reverse proxy headers to get information about the original request when it’s behind a proxy.
ForwardedHeaderSupporthandles the standard
Forwardedheader (RFC 7239)
XForwardedHeaderSupporthandles the non-standard (but standard de-facto)
Only install these features if you have a reverse proxy supporting these headers serving your requests. In other cases, a client will be able to manipulate these headers.
Table of contents:
io.ktor.features.ForwardedHeaderSupportand no additional artifacts are required.
These features don’t require any special configuration.
You can install any of the two depending on your reverse proxy,
but since the standard is the
Forwarded header, you should
favor it whenever possible.
You can see all the available request properties on the Requests page.
You can read the raw or local request information, read from the received normal
headers and socket properties, that correspond to the proxy request
val scheme = request.local.scheme val version = request.local.version val port = request.local.port val host = request.local.host val uri = request.local.uri val method = request.local.method val remoteHost = request.local.method
You can read the original request information, read from the
X-Forwarded-* headers with fallback to the raw headers,
that corresponds to original client request using the
val scheme = request.origin.scheme // Determined from X-Forwarded-Proto / X-Forwarded-Protocol / X-Forwarded-SSL val version = request.origin.version val port = request.origin.port // Determined from X-Forwarded-Host / X-Forwarded-Server val host = request.origin.host // Determined from X-Forwarded-Host / X-Forwarded-Server val uri = request.origin.uri val method = request.origin.method val remoteHost = request.origin.method // Determined from X-Forwarded-For
In the cases where you need the X-Forwarded-By (the interface used for the socket), you can access the raw X-Forwarded properties with:
val forwardedValues: List<ForwardedHeaderSupport.ForwardedHeaderValue> = call.attributes[ForwardedHeaderSupport.ForwardedParsedKey]
data class ForwardedHeaderValue(val host: String?, val by: String?, val forParam: String?, val proto: String?, val others: Map<String, String>)
Forwarded header looks like this:
Forwarded: by=<identifier>; for=<identifier>; host=<host>; proto=<http|https>
by- The interface where the request came in to the proxy server.
for- The client that initiated the request and subsequent proxies in a chain of proxies.
host- The Host request header field as received by the proxy.
proto- Indicates which protocol was used to make the request (typically “http” or “https”).
You can read more about Forwarded in the MDN documentation.