I have used Keycloak in its very early stage ( when it is was in 2.x version). But now it has come a long way (at this time of writing it is in 21.x)
In this article let's configure Keycloak behind Nginx. Here are the points to consider.
If you want to configure Apache2 as a proxy server for your java application, please check this article.
- We are going to use a domain name other than localhost
- Anything other than localhost will require Keycloak to run in production mode which requires SSL configurations etc.
- Or it requires a proxy server.
Lets begin.
Requirements
- Keycloak distribution
- Ubuntu 22.04 server
Configuring Keycloak
1. Download Keycloak from here.
2. Extract it using tar -xvzf keycloak-21.0.1.tar.gz
3. Create a script file called keycloak.sh with the following contents
#!/bin/bash
export KEYCLOAK_ADMIN=<admin-username-here>
export KEYCLOAK_ADMIN_PASSWORD=<admin-password-here>
nohup keycloak-21.0.0/bin/kc.sh start-dev --proxy edge --hostname-strict=false --hostname=auth.mydomain.com &
4. Start the server with sh keycloak.sh
Configure Nginx and certbot
Certbot is needed to procure and install the ssl certificates.
1. Install nginx
sudo apt-get update
sudo apt-get install nginx
2. Check the status of nginx server.
systemctl status nginx
3. If it is not started, restart it
sudo systemctl restart nginx
4. Create a site file under /etc/nginx/sites-available/mysite with the following content.
server {
server_name auth.mydomain.com;
root /var/www/html;
index index.html;
access_log /var/log/nginx/mydomain-access.log;
error_log /var/log/nginx/mydomain-error.log;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8080;
# the following headers are needed, if your application uses redirection flow to authenticate with Keycloak.
# replace localhost:8084 with the application server url
add_header Content-Security-Policy "frame-src *; frame-ancestors *; object-src *;";
add_header Access-Control-Allow-Origin 'http://localhost:8084';
add_header Access-Control-Allow-Credentials true;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/auth.mydomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/auth.mydomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
5. Enable the site using sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite
6. Comment the following lines and restart nginx.
#ssl_certificate /etc/letsencrypt/live/auth.primebyte.in/fullchain.pem; # managed by Certbot
#ssl_certificate_key /etc/letsencrypt/live/auth.primebyte.in/privkey.pem; # managed by Certbot
#include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
7. Install the certificates
sudo certbot --nginx
Follow the instructions to compelte the SSL certificate generation and installation
8. Uncomment the following lines and restart nginx
ssl_certificate /etc/letsencrypt/live/auth.primebyte.in/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/auth.primebyte.in/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
If you see that admin page is throwing error and observe the following in the logs, it is due the buffer memory issues.
2023/03/02 10:20:20 [error] 1747#1747: *160 upstream sent too big header while reading response header from upstream...
It can be fixed by add the following lines under http section of /etc/nginx/nginx.conf.
http {
...
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
Restart nginx.
Using Nginx as a proxy server for Keycloak is a smart choice for enhancing security and performance. It efficiently handles authentication flows, improving user experience and simplifying access management. Your guide on configuring Nginx to proxy requests to Keycloak is clear and practical, making it accessible even for those new to server setups. This setup not only ensures seamless integration but also boosts the reliability and scalability of applications relying on Keycloak for identity and access management.
ReplyDelete