How to get response body when using proxy_intercept_errors

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get response body when using proxy_intercept_errors

vergil
I have a requirement where when proxied response error code is 401
(condition #1) and response body has text "SAML Token Expired" (condition
#2), I need to intercept it and redirect to refresh token API.

I could do it partially where only error code 401 is considered. Not able to
find a way to read response body text to give condition #2:

"location /service {
   proxy_intercept_errors on;
   error_page 401 = @refresh;
}
location @refresh {

   (# Here check if the response body has text "SAML Token Expired". If yes
return 401 directly.)

   set $original_uri $scheme://$http_host$request_uri;
   return 307
https://localhost:8083/service/auth/refresh?uri=$original_uri;
}"

Please help me on getting the response body text. Thanks in advance!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,288683,288683#msg-288683

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: How to get response body when using proxy_intercept_errors

Maxim Dounin
Hello!

On Tue, Jul 14, 2020 at 04:25:46AM -0400, salmanpskply wrote:

> I have a requirement where when proxied response error code is 401
> (condition #1) and response body has text "SAML Token Expired" (condition
> #2), I need to intercept it and redirect to refresh token API.
>
> I could do it partially where only error code 401 is considered. Not able to
> find a way to read response body text to give condition #2:

There is no way.  Errors are intercepted based on the response
headers, and the response body is not read from the upstream
server if the error is intercepted.

If you need to analyze the response body, consider using njs
instead.  Doing appropriate subrequest and looking into it might
work for you, see here for details:

http://nginx.org/en/docs/http/ngx_http_js_module.html
http://nginx.org/en/docs/njs/reference.html#subrequest

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: How to get response body when using proxy_intercept_errors

vergil
Thank you

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,288683,288693#msg-288693

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: How to get response body when using proxy_intercept_errors

Desiree Valdez
In reply to this post by Maxim Dounin
Stop emails to me

On Tue, Jul 14, 2020, 6:13 AM Maxim Dounin <[hidden email]> wrote:
Hello!

On Tue, Jul 14, 2020 at 04:25:46AM -0400, salmanpskply wrote:

> I have a requirement where when proxied response error code is 401
> (condition #1) and response body has text "SAML Token Expired" (condition
> #2), I need to intercept it and redirect to refresh token API.
>
> I could do it partially where only error code 401 is considered. Not able to
> find a way to read response body text to give condition #2:

There is no way.  Errors are intercepted based on the response
headers, and the response body is not read from the upstream
server if the error is intercepted.

If you need to analyze the response body, consider using njs
instead.  Doing appropriate subrequest and looking into it might
work for you, see here for details:

http://nginx.org/en/docs/http/ngx_http_js_module.html
http://nginx.org/en/docs/njs/reference.html#subrequest

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx