Keycloak is an open-source identity and access management solution that enables secure login for applications and services. It supports Single Sign-On (SSO), identity brokering, and more. This article will guide you through securing your Nginx web server using Keycloak for authentication and authorization.
Prerequisites
Before securing Nginx with Keycloak, ensure that the following prerequisites are in place:
- Keycloak server is installed and running (either self-hosted or using a Keycloak cloud service).
- Nginx is installed and running on your server.
- A registered realm and client configured in Keycloak for your application.
Step 1: Install Nginx and Keycloak Proxy Module
To integrate Keycloak with Nginx, you need to install the nginx-keycloak
module or use an OAuth2 proxy to handle the authentication process.
Install Nginx
sudo apt update sudo apt install nginx
Install OAuth2 Proxy
Step 2: Configure Keycloak
Keycloak needs to be configured to authenticate users for your Nginx server. Here’s how to set it up:
Create a Client in Keycloak
- Log in to your Keycloak admin console.
- Navigate to the Clients section.
- Click Create and enter a name for your client (e.g.,
nginx-client
). - Set the Client Protocol to
openid-connect
. - Enter the Redirect URI for your code that you want to protect it can be google.com , http://127.0.0.1:8080 etc
- Save the client configuration.
Configure Client Settings
- Under the Settings tab, configure the Access Type to
confidential
.- if there is no confidential then turn on the Client authentication
- Generate a Client Secret and note it down for later use.
- Ensure that the Standard Flow Enabled and Implicit Flow Enabled options are enabled.
Step 3: Configure OAuth2 Proxy
Now, configure the OAuth2 proxy to communicate with your Keycloak server.
Configure OAuth2 Proxy
In your OAuth2 proxy configuration file, you will need to set the Keycloak details:
provider = "keycloak"
client_id = "nginx-client"
client_secret = "your-client-secret"
redirect_url = "http://your-nginx-server.com/oauth2/callback"
email_domains = ["*"]
upstream = "http://127.0.0.1:80"
cookie_secret = "random-cookie-secret"
cookie_secure = false
cookie_domains = "your-nginx-server.com"
http_address = "127.0.0.1:4180"
“The upstream
configuration in OAuth2 Proxy points to your code server. This can be set to the default localhost or to a specific IP address where your code is hosted, such as http://127.0.0.1:8080
.
The http_address
setting defines the IP address and port where the OAuth2 Proxy service will listen. Configure it to match the desired address and port combination for your setup.
Run OAuth2 Proxy
Start the OAuth2 proxy with the following command:
oauth2-proxy --config /etc/oauth2-proxy/oauth2-proxy.cfg
Step 4: Configure Nginx
Next, configure Nginx to work with the OAuth2 proxy. This involves setting up Nginx as a reverse proxy to forward requests to the OAuth2 proxy, and ensuring that authentication is required for all access to the protected content.
Edit Nginx Configuration
Edit your Nginx configuration file to include the OAuth2 proxy as a reverse proxy:
server { listen 80; location / { # Proxy to OAuth2 Proxy proxy_pass http://127.0.0.1:4180; # OAuth2 Proxy default port proxy_set_header Host $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-Proto $scheme; } }
Set Up Nginx SSL (Optional)
If you want to secure the traffic with HTTPS, you can configure SSL in your Nginx setup. Here’s a basic SSL configuration:
server { listen 443 ssl; ssl_certificate /etc/ssl/certs/your_domain.crt; ssl_certificate_key /etc/ssl/private/your_domain.key; location / { proxy_pass http://127.0.0.1:4180; proxy_set_header Host $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-Proto $scheme; } }
Step 5: Restart Nginx and OAuth2 Proxy
Restart both Nginx and the OAuth2 Proxy to apply the changes:
sudo systemctl restart nginx sudo systemctl restart oauth2-proxy
Step 6: Test the Integration
Finally, visit your Nginx server’s URL (e.g., http://your-nginx-server.com
) in a web browser. You should be redirected to the Keycloak login page. After successfully logging in, you will be redirected back to your Nginx server with access granted based on the Keycloak authentication.
Conclusion
Securing Nginx with Keycloak is an efficient way to handle authentication and authorization in your applications. By using OAuth2 proxy, you can integrate Keycloak with Nginx to enforce secure login protocols. This setup ensures that only authenticated users can access your server, adding a robust layer of security to your web applications.