Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify HTTP status code of proxy result in nginx

I have a nginx reverse proxy serving a web application. This web app returns a 302 redirect in some cases and I can't change it. However, for some reasons I need to change this returned status to be 301.

I tried

proxy_intercept_errors on;
error_page 302 =301;

but obviously this the =301 part as the new location. So what I'm looking for would be something like this:

proxy_intercept_errors on;
error_page 302 =301 $PROXY_HEADER_LOCATION;

How can I do this?

like image 478
Scolytus Avatar asked Nov 01 '25 14:11

Scolytus


1 Answers

The header fields of the upstream server are accessible through $upstream_http_*

So this worked for me:

proxy_intercept_errors on;
error_page 302 =301 $upstream_http_location;
like image 144
Scolytus Avatar answered Nov 04 '25 04:11

Scolytus