NGiNX auth_request set-cookie for use downstream?

大兔子大兔子 提交于 2020-01-02 07:27:20

问题


Trying to create a cookie inside the auth_request call and pass it downstream for immediate use. The best I can manage so far is setting the cookie in the browser after the call is complete with:

auth_request /blah/blah;
auth_request_set $saved_set_cookie $upstream_http_set_cookie;
add_header Set-Cookie $saved_set_cookie;

That cookie will not be available downstream until the next request. I can always get the new cookie by doing the following, but it's not ideal to have it in a new header name:

auth_request_set $saved_cookie_try_2 $upstream_cookie_COOKIE_NAME;
proxy_set_header X-Fake-Cookie $saved_cookie_try_2;

This allows the cookie value to be immediately available as a custom header, but isn't as convenient as just having the cookie.

Any suggestions?

Update 1:

I was able to add to the existing cookies or replace them entirely with:

proxy_set_header Cookie "$http_cookie; $saved_set_cookie";
or
proxy_set_header Cookie "$saved_set_cookie";

I was not able to figure out a way to set or replace only a single cookie.

Update 2:

I can probably use NGiNX map function for this task:

map $http_cookie $auth_header {
  default "";
  "~*yourCookieName=(?<variable>[^;]+)" "the value you wanna set 
  $variable";
}

From here: https://stackoverflow.com/a/45208334/1502561

来源:https://stackoverflow.com/questions/48177119/nginx-auth-request-set-cookie-for-use-downstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!