2022-01-27 10:17:22 +00:00
<#import "/templates/guide.adoc" as tmpl>
<#import "/templates/kc.adoc" as kc>
<#import "/templates/links.adoc" as links>
<@tmpl.guide
2022-02-04 15:02:22 +00:00
title="Configuring the hostname"
2022-01-27 10:17:22 +00:00
summary="Learn how to configure the frontend and backchannel endpoints exposed by Keycloak."
includedOptions="hostname-* proxy">
2022-02-03 16:20:55 +00:00
When running Keycloak in environments such as Kubernetes, OpenShift, or on-premise, you want to protect the internal URLs from exposure to the public facing internet.
2022-04-07 06:07:12 +00:00
Instead, You want to expose your public hostname.
2022-02-03 16:20:55 +00:00
This guide describes how to configure Keycloak to use the right hostname for different scenarios.
2022-01-27 10:17:22 +00:00
2022-04-07 06:07:12 +00:00
== Hostname Syntax:
Keycloak does not impose strict validation for the configured hostname value. Please make sure the hostname value you configure complies to the standardized hostname syntax as outlined in RFC 952, RFC 1123 and others.
.Example:
`LOCALHOST` is a non-compliant hostname and leads to problems when the browser tries to resolve your requests. Instead, `localhost` will work fine.
2022-01-27 10:17:22 +00:00
== Keycloak API Endpoint categories
2022-02-03 16:20:55 +00:00
As this section explains, Keycloak exposes three API endpoint categories: frontend, backend, and administrative.
Each category uses a specific base URL.
2022-01-27 10:17:22 +00:00
=== Frontend Endpoints
2022-02-03 16:20:55 +00:00
Frontend endpoints are used to externally access Keycloak.
When no hostname is set, the base URL used for the frontend is taken from the incoming request.
This choice has some major disadvantages.
For example, in a high availability scenario, you may have multiple Keycloak instances.
The choice of URL should not depend on the instance where the request lands.
The URL should be used for all instances, so they are seen as one system from the outside.
2022-01-27 10:17:22 +00:00
2022-02-03 16:20:55 +00:00
To set the hostname part of the frontend base URL, enter this command:
2022-01-27 10:17:22 +00:00
<@kc.start parameters="--hostname=<value>"/>
2022-03-22 07:11:50 +00:00
You can also set a different port if your proxy is exposing the frontend URL using a port other than the default HTTP (80) and HTTPS(443) ports. For that,
set the `hostname-port` option.
<@kc.start parameters="--hostname=<value> --hostname-port=<port>"/>
2022-01-27 10:17:22 +00:00
=== Backend Endpoints
2022-02-03 16:20:55 +00:00
Backend endpoints are used for direct communication between Keycloak and applications.
Examples of backend endpoints are the Token endpoint and the User info endpoint.
Backend endpoints are also taking the base URL from the request by default.
To override this behavior, set the `hostname-strict-backchannel` configuration option by entering this command:
2022-01-27 10:17:22 +00:00
<@kc.start parameters="--hostname=<value> --hostname-strict-backchannel=true"/>
2022-02-03 16:20:55 +00:00
When all applications connected to Keycloak communicate through the public URL, set `hostname-strict-backchannel` to true.
Otherwise, leave this parameter as false to allow internal applications to communicate with Keycloak through an internal URL.
2022-01-27 10:17:22 +00:00
=== Administrative Endpoints
2022-02-03 16:20:55 +00:00
To reduce attack surface, the administration endpoints for Keycloak and the Admin Console should not be publicly accessible.
Therefore, you can secure them by using a reverse proxy.
2022-04-27 09:41:19 +00:00
For more information about which paths to expose using a reverse proxy, see the <@links.server id="reverseproxy"/> Guide.
2022-01-27 10:17:22 +00:00
== Overriding the hostname path
2022-02-03 16:20:55 +00:00
When running Keycloak behind a reverse proxy, you may expose Keycloak using a different context path such as `myproxy.url/mykeycloak`.
To perform this action, you can override the hostname path to use the path defined in your reverse proxyas shown in this example:
2022-01-27 10:17:22 +00:00
<@kc.start parameters="--hostname=myurl --hostname-path=mykeycloak"/>
2022-02-03 16:20:55 +00:00
The `hostname-path` configuration takes effect when a reverse proxy is enabled.
2022-04-27 09:41:19 +00:00
For details, see the <@links.server id="reverseproxy"/> Guide.
2022-01-27 10:17:22 +00:00
2022-05-06 09:59:55 +00:00
== Accessing Keycloak in production mode using HTTP
2022-05-10 07:15:47 +00:00
When a `hostname` is set and the server is running in production mode, all the URLs generated by the server are going to use the `HTTPS` scheme. If you are not setting up TLS you might run into issues because some URLs generated by the server won't work.
Keycloak follows the "secure by design" principle, so it is absolutely not recommended to access Keycloak without proper transport encryption, as this opens up multiple attack vectors.
2022-05-06 09:59:55 +00:00
Nevertheless there are environments, where Keycloak is deployed behind a proxy/load balancer that terminates TLS completely and the internal requests are done using the unencrypted HTTP protocol.
To be able to work with Keycloak using HTTP for these environments, there is the hidden configuration option `hostname-strict-https=<true/false>`. This option is set to `true` by default for the production mode, and `false` for the development mode.
When you need to access Keycloak using HTTP in production mode, for example when you use `proxy=edge` and you want to access the administration console internally using HTTP, you have to set `hostname-strict-https=false`, otherwise a blank page will show up.
Keep in mind the recommended approach is to always use HTTPS, and this still is true for external clients.
2022-01-27 10:17:22 +00:00
== Using the hostname in development mode
2022-02-03 16:20:55 +00:00
You run Keycloak in development mode by using `start-dev`.
In this mode, the hostname setting is optional.
When it is omitted, the incoming request headers are used.
2022-01-27 10:17:22 +00:00
=== Example: Hostname in development mode
.Keycloak configuration:
<@kc.startdev parameters=""/>
.Invoked command:
[source, bash]
----
curl GET "https://localhost:8080/realms/master/.well-known/openid-configuration" | jq .
----
.Result:
[source, bash]
----
# Frontend endpoints: request://request:request -> http://localhost:8080
# Backend endpoints: request://request:request -> http://localhost:8080
----
2022-02-03 16:20:55 +00:00
In this example of using a curl GET request, the result shows the current OpenID Discovery configuration.
All base URLS are taken from the incoming request, so `http://localhost:8080` is used for all endpoints.
2022-01-27 10:17:22 +00:00
== Example Scenarios
2022-02-02 22:36:50 +00:00
The following are more example scenarios and the corresponding commands for setting up a hostname.
2022-01-27 10:17:22 +00:00
=== Assumptions for all scenarios
* Keycloak is set up using HTTPS certificates and Port 8443.
2022-02-02 22:36:50 +00:00
* `intlUrl` refers to an internal IP/DNS for Keycloak.
2022-01-27 10:17:22 +00:00
* `myUrl` refers to an exposed public URL
2022-02-02 22:36:50 +00:00
* Keycloak runs in production mode using the `start` command.
2022-01-27 10:17:22 +00:00
=== Example 1: Hostname configuration without reverse proxy
.Keycloak configuration:
<@kc.start parameters="--hostname=myurl"/>
.Invoked command:
[source, bash]
----
curl GET "https://intUrl:8443/realms/master/.well-known/openid-configuration" | jq .
----
.Result:
[source, bash]
----
# Frontend Endpoints: request://myurl:request -> https://myurl:8443
# Backend Endpoints: request://request:request -> https://internal:8443
----
=== Example 2: Hostname configuration without reverse proxy - strict backchannel enabled
.Keycloak configuration:
<@kc.start parameters="--hostname=myurl --hostname-strict-backchannel=true"/>
.Invoked command:
[source, bash]
----
curl GET "https://intUrl:8443/realms/master/.well-known/openid-configuration" | jq .
----
.Result:
[source, bash]
----
# Frontend: request://myurl:request -> https://myurl:8443
# Backend: request://myurl:request -> https://myurl:8443
----
=== Example 3: Hostname configuration with reverse proxy
.Keycloak configuration:
<@kc.start parameters="--hostname=myurl --proxy=passthrough"/>
.Invoked command:
[source, bash]
----
curl GET "https://intUrl:8443/realms/master/.well-known/openid-configuration" | jq .
----
.Result:
[source, bash]
----
# Frontend Endpoints: request://myurl -> https://myurl
# Backend Endpoints: request://request:request -> https://internal:8443
----
=== Hostname configuration with reverse proxy and different path
.Keycloak configuration:
<@kc.start parameters="--hostname=myurl --proxy=passthrough --hostname-path=mykeycloak"/>
.Invoked command:
[source, bash]
----
curl GET "https://intUrl:8443/realms/master/.well-known/openid-configuration" | jq .
----
.Result:
[source, bash]
----
# Frontend Endpoints: request://myurl -> https://myurl/mykeycloak
# Backend Endpoints: request://request:request -> https://internal:8443
----
</@tmpl.guide>