nginx登录背后的keycloak失败,帖子中缺少端口号等
Posted
技术标签:
【中文标题】nginx登录背后的keycloak失败,帖子中缺少端口号等【英文标题】:keycloak behind nginx login fails, port number missing from post etc 【发布时间】:2018-04-04 03:14:25 【问题描述】:Keycloak 丢失进入页面时传递的端口号:30666
但是,提交按钮不包含 ip+端口号,此处仅使用 ip-address。由于帖子失败。
重定向失败...
如何让 Keycloak 在代理后工作?
keycloak 在 nginx 代理后面的 kubernetes 集群中运行,配置如下:
worker_processes 1;
error_log /dev/stderr warn;
events
worker_connections 1024;
# make sure to set plaintext JWT_SECRET environment variable
env JWT_SECRET;
http
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stdout main;
lua_package_path "/usr/local/openresty/lualib/?.lua;;";
server
listen 8080;
root /;
# load index page from nginx implementing the KC javascript:
location /
index index.htm index.html;
location /auth
proxy_pass http://idp:8080/auth;
proxy_http_version 1.1; # this is essential for chunked responses to work
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
# Secured endpoints
location /secure/
access_by_lua_file /bearer.lua;
default_type text/plain;
echo "<p>i am protected by jwt<p>";
我的 idp 部署如下所示:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert -f docker-compose.yml
kompose.version: 1.2.0 ()
creationTimestamp: null
labels:
io.kompose.service: idp
name: idp
spec:
replicas: 1
strategy:
template:
metadata:
creationTimestamp: null
labels:
io.kompose.service: idp
spec:
containers:
- env:
- name: KEYCLOAK_PASSWORD
value: pass
- name: KEYCLOAK_USER
value: admin
- name: PROXY_ADDRESS_FORWARDING
value: 'true'
image: jboss/keycloak
name: idp
ports:
- containerPort: 9990
- containerPort: 8080
resources:
restartPolicy: Always
status:
【问题讨论】:
今天我将在 ingress-nginx 或类似名称后面运行 Keycloak 实例。这样配置更容易。 【参考方案1】:问题是proxy_set_header $host
,应该是$host:$server_port
此外,不需要以代理 URL 为后缀的 /auth URI。如果未指定,则 Nginx 将传输 URI 而不更改它。
配置应该是:
location /auth
proxy_pass http://idp:8080;
...
proxy_set_header Host $host:$server_port;
参考http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
注意:Keycloak 客户端可能需要 HTTPS URL。如果您在 Nginx 中启用 HTTPS,请记住还要使用 x-forwarded-proto 标头将方案传递给 Keycloak。
proxy_set_header x-forwarded-proto $scheme;
【讨论】:
是的!proxy_set_header Host $host:$server_port;
为我成功了。谢谢!
而proxy_set_header Host $http_host;
工作得更好......(反映了 docker 端口映射)【参考方案2】:
您还必须添加以下标题
proxy_set_header X-Forwarded-Port $server_port;
【讨论】:
以上是关于nginx登录背后的keycloak失败,帖子中缺少端口号等的主要内容,如果未能解决你的问题,请参考以下文章