I want to intercept nginx response and based on the status code, have to add a cookie

I have to intercept the response from the upstream before sending it to the client and if the response is 302 – I have to add a cookie.

I tried using the following snippets

    error_code = 302 @add_cookie
location @add_cookie {
# add the cookie 
return 302 $upstream_http_location
}

This is taking me to the client but not the actual redirect url given by the server and all the cookies , headers were removed.

How to add a cookie to the response before sending to the client in nginx?

Read more here: Source link