I'd like to have Nginx act as a reverse proxy for a backend service, but the connection to the backend service must itself go through another proxy:
[nginx] -> [HTTP proxy] -> [backend service]
It does not appear that Nginx has a way to configure that the outgoing connection passes through a proxy (e.g., the http_proxy
environment variable is ignored).
But it might be sufficient to:
Set the
proxy_pass
destination to the HTTP proxy server address.Convince Nginx send the complete target URL to the proxy as in
GET http://backend/path/etc
instead of justGET /path/etc
.
I tried:
rewrite ^/prefix/(.*) http://backend/$1 break;
proxy_pass http://proxy;
which almost works, but the behavior of rewrite
is that it stops processing and issues a redirect if the replacement begins with http://
. (Code here).
CodePudding user response:
There is a hacky way to work around the redirecting behavior of rewrite
by using two rewrite
directives:
location /prefix/ {
rewrite ^/prefix/(.*) xxx://backend/$1 ;
rewrite ^xxx(.*) http$1 break;
proxy_pass http://proxy;
}
An incoming GET request to http://frontend/prefix/x
would therefore cause a connection to the proxy server and send GET http://backend/x
which would be forwarded to the backend server as GET /x
.