[英]Keycloak behind reverse proxy
我在服務器上安裝了keycloak standanlone,並嘗試通過nginx在反向代理后面使用它。 密鑰斗篷已綁定到127.0.0.1
那是我的nginx vhost配置:
server {
server_name auth.dp.net;
location /auth {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
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;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/auth.dp.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/auth.dp.net/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
}
server {
if ($host = auth.dp.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name auth.dp.net;
return 404; # managed by Certbot
}
但是當我訪問https://auth.dp.net/auth時,出現以下密鑰泄漏錯誤:
Invalid parameter: redirect_uri
我的配置中缺少什么?
一個/
或兩個,也許一個=
。
location /auth {
proxy_pass http://localhost:8080;
如果您的代理通過指令為裸機,即未指定路徑(如上所示),則整個請求路徑將附加到代理通過網址。
因此,您的上述配置將導致對https://auth.dp.net/auth
的請求被代理到http://localhost:8080/auth/
。
如果您添加任何內容,甚至在代理傳遞指令中僅添加一個斜線,那么您添加的任何內容都會替換您位置指令中匹配的部分。 因此,要將請求傳遞到沒有路徑的代理,您需要添加一個斜杠。 所以這可能工作:
location /auth {
proxy_pass http://localhost:8080/;
但是,由於Nginx喜歡在代理時在請求網址不包含一個的情況下添加一個斜杠,因此它可能無法工作。 因此,您可以通過將location指令更改為:來告訴Nginx完全匹配:
location = /auth {
或者,您可以預料到它將要重寫您的請求並將其更改為:
location /auth/ {
或者,如果您真的想成為專業人士,則可以創建兩個相同的模塊。 一個使用= /auth
,另一個使用/auth/
這是因為:
= /auth
則對https://auth.dp.net/auth/
的請求將不匹配,因此不會被代理。 /auth/
則它將同時適用於https://auth.dp.net/auth
和https://auth.dp.net/auth/
但前者將觸發Nginx內部的內部重寫以添加/
,有效地將身份驗證請求的服務器負載加倍。 轉到Keycloak客戶端的設置,然后將有效重定向Uri更改為*。
有效的重定向Uri是Keycloak的一種安全機制,它限制了重定向的發生位置。 在生產中,應盡可能精確地確保安全連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.