2016-11-06 36 views
0

나는 몇 가지 서비스를 가지고 있으며 그들은 nginx 인스턴스 뒤에 서있다. nginx에서 인증을 처리하기 위해 각 요청을 가로 채서 인증 서비스로 보냅니다. 거기에, 자격 증명이 올바른지, 나는 사용자 관련 정보를 포함하는 쿠키를 설정하고 있습니다.nginx 프록시 인증 요격

요청은 이제 쿠키가 설정된 적절한 서비스로 라우팅되어야합니다.

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"; 
    } 
} 

내가 수동으로 설정 한 X-대상 헤더의 요청에/인증 프록시를 한 경우는, 응답이 예상대로 쿠키를 포함

여기 내의 nginx의 설정입니다.

원하는 대상에 요청하면 요청이 가로 채고 쿠키를 올바르게 설정하는/auth-proxy에 도달합니다. 그러나 요청이 대상에 도달하면 쿠키가 포함되지 않습니다.

대상 요청을 수행 할 때 nginx가 쿠키를 전달하지 않는다고 가정합니다.

저는 지난 며칠 동안이 문제로 고생했습니다 ... 나는 무엇을 놓치고 있습니까?

감사합니다.

답변

1

나는 마침내 그것을 알아 냈습니다. auth_request_set을 사용하여 auth 응답에서 쿠키를 읽었으며 호출자에 대한 응답과 대상에 대한 후속 요청에서 모두 수동으로 설정했습니다.

if is evil이므로 lua에서 검사를 추가했습니다.

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; 
    } 
}