# Set\_encrypt\_session after access phase?

**URL:** <https://www.ruby-forum.com/t/set-encrypt-session-after-access-phase/242379>\
**Category:** NGINX\
**Created:** [June 19, 2015, 4:21pm UTC](https://www.ruby-forum.com/t/set-encrypt-session-after-access-phase/242379 "2015-06-19T16:21:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [June 19, 2015, 4:21pm UTC](https://www.ruby-forum.com/t/set-encrypt-session-after-access-phase/242379/1 "2015-06-19T16:21:20Z")

</div>

On Thu, Jun 18, 2015 at 11:29 AM, Vader M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m having trouble setting a cookie conditionally based upon  
> an upstream variable The hope is to cache an auth token in an  
> encrypted session and only go to the backend auth token generator once.

I managed to figure out how to use map to set the cookie:

```
map $new_auth_tok $cond_cookie_k {
    '' '';
    default "my_login=";
}

map $new_auth_tok $cond_cookie_v {
    '' '';
    default $b32_session;
}

add_header Set-Cookie $cond_cookie_k$cond_cookie_v;

```

However, my problem is that set\_encrypt\_session actually runs in the  
rewrite phase before my authentication back end like this:

```
location / {
    root /var/www;
    index index.html index.htm;

    set_encrypt_session $enc_auth_tok $new_auth_tok;
    set_encode_base32 $b32 $enc_auth_tok;

    auth_request /auth;
    auth_request_set $new_auth_tok $upstream_http_auth_tok;

    add_header Set-Cookie $cond_cookie_k$cond_cookie_v;
}

```

Is there any way to encrypt after the access phase?
