nginx登录后面的keycloak失败,帖子中缺少端口号等

时间:2017-10-23 14:17:17

标签: nginx proxy keycloak

Keycloak在输入页面时失去了保留的端口号: 30666

然而,提交按钮不包含ip +端口号,此处使用ip-address。由于帖子失败了。

重定向失败......

如何让Keycloak在代理服务器后面工作?

enter image description here enter image description here enter image description here enter image description here

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: {}

2 个答案:

答案 0 :(得分:4)

问题是proxy_set_header $host,应该是$host:$server_port

此外,不需要代理URL后缀的/ auth URI。如果未指定,则Nginx将在不更改URI的情况下传输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;

答案 1 :(得分:0)

您还必须添加以下标头

proxy_set_header    X-Forwarded-Port   $server_port;