2016-11-06 11 views
0

Ich habe ein paar Service und sie stehen hinter einer Nginx-Instanz. Um mit der Authentifizierung fertig zu werden, unterteile ich in nginx jede Anfrage und sende sie an den Authentifizierungsdienst. Dort, wenn die Zugangsdaten korrekt sind, setze ich ein Cookie, das benutzerbezogene Informationen enthält.Nginx-Proxy-Authentifizierung abfangen

Die Anforderung sollte jetzt mit dem gesetzten Cookie an den entsprechenden Dienst weitergeleitet werden.

Hier ist meine nginx config:

user nginx; 
worker_processes 1; 

error_log /var/log/nginx/error.log warn; 
pid  /var/run/nginx.pid; 

events { 
    worker_connections 1024; 
} 

http { 
    upstream xyz { 
    server ***; 
    } 

    upstream auth { 
    server ***; 
    } 

    server { 
    listen  8080; 
    location ~ ^/(abc|xyz)/api(/.*)?$ { 
    auth_request /auth-proxy; 

    set $query $2; 

    proxy_pass http://$1/api$query$is_args$args; 
    proxy_set_header X-Target $request_uri; 
    proxy_set_header Host $http_host; 
    } 

    location = /auth-proxy { 
    internal; 
    proxy_pass http://auth; 

    proxy_pass_request_body off; 
    proxy_set_header Content-Length ""; 
    proxy_set_header X-Target $request_uri; 
    proxy_set_header Host $http_host; 
    proxy_set_header X-CookieName "auth"; 
    proxy_set_header Cookie "auth=$cookie_auth"; 
    proxy_set_header Set-Cookie "auth=$cookie_auth"; 
    proxy_cookie_path/"/; Secure; HttpOnly"; 
    add_header Cookie "auth=$cookie_auth"; 
    add_header Set-Cookie "auth=$cookie_auth"; 
    } 
} 

Wenn ich eine Anfrage an/Auth-Proxy mit einem x-Ziel-Header gesetzt manuell machen, die Antwort enthält das Cookie wie erwartet.

Wenn ich eine Anfrage an das gewünschte Ziel mache, wird die Anfrage abgefangen, sie erreicht/auth-proxy, die den Cookie korrekt setzt. Wenn die Anforderung jedoch das Ziel erreicht, enthält sie das Cookie nicht.

Ich nehme an, dass Nginx den Cookie nicht weiterleitet, wenn die Zielanforderung ausgeführt wird.

Ich habe in den letzten paar Tagen damit zu kämpfen ... was fehlt mir?

Danke!

Antwort

1

Ich habe es endlich herausgefunden. Ich habe auth_request_set verwendet, um den Cookie von der Auth-Antwort zu lesen, und ich habe es manuell sowohl auf die Antwort auf den Anrufer als auch auf die nachfolgende Anfrage an das Ziel gesetzt.

Da if is evil, habe ich den Check-in lua hinzugefügt.

server { 
    listen  8080; 
    location ~ ^/(abc|xyz)/api(/.*)?$ { 
    auth_request /auth-proxy; 

    # read the cookie from the auth response 
    auth_request_set $cookie $upstream_cookie_auth; 
    access_by_lua_block { 
     if not (ngx.var.cookie == nil or ngx.var.cookie == '') then 
     ngx.header['Set-Cookie'] = "auth=" .. ngx.var.cookie .. "; Path=/" 
     end 
    } 
    # add the cookie to the target request 
    proxy_set_header Cookie "auth=$cookie"; 

    set $query $2; 

    proxy_pass http://$1/api$query$is_args$args; 
    proxy_set_header X-Target $request_uri; 
    proxy_set_header Host $http_host; 
    } 
}