Forwarded headers
The ForwardedHeaders and XForwardedHeaders plugins allow you to handle reverse proxy headers to get information about the original request when a Ktor server is placed behind a reverse proxy. This might be useful for logging purposes.
ForwardedHeaders
handles theForwarded
header (RFC 7239)XForwardedHeaders
handles the followingX-Forwarded-
headers:X-Forwarded-Host
/X-Forwarded-Server
X-Forwarded-For
X-Forwarded-By
X-Forwarded-Proto
/X-Forwarded-Protocol
X-Forwarded-SSL
/Front-End-Https
Add dependencies
To use the ForwardedHeaders
/XForwardedHeaders
plugins, you need to include the ktor-server-forwarded-header
artifact in the build script:
Install plugins
To install the ForwardedHeaders
plugin, pass it to the install
function in the application initialization code. Depending on the way used to create a server, this can be the embeddedServer
function call ...
... or a specified module.
To install the XForwardedHeaders
plugin, pass it to the install
function in the application initialization code. Depending on the way used to create a server, this can be the embeddedServer
function call ...
... or a specified module.
ForwardedHeaders
and XForwardedHeaders
don't require any special configuration.
Get request information
Proxy request information
To get information about the proxy request, use the call.request.local
property inside the route handler. The code snippet below shows how to obtain information about the host and port:
Original request information
To read information about the original request, use the call.request.origin
property:
You can find the full example here: forwarded-header.