Background
I have two instances of a web application which I want accessible at mydomain.com/
and mydomain.com/staging/
. I have configured nginx to proxy requests to the proper instance depending on the path, but when a link is clicked in the staging instance, the path (in the HTML) refers to the non-staging application.
What I have
I want to configure nginx to proxy requests to the staging environment whenever an http request is made with referrer mydomain.com/staging/...
. I have this much working.
What I need
I need to redirect the request to the proper /staging url. I cannot figure out how to get (example.com/<all this>
) the path out of the requested url and append it to my /staging path for redirection. Here is the relevant portion of my config.
location @puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
# if mydomain.com/products/1 is requested with a /staging referrer
# I need to redirect to /staging/products/1 instead of always /staging/
if ($http_referer ~* /.*\.com/staging$ ) {
set $request_url /staging/;
return 301 $request_url;
}
proxy_pass http://puma_production;
}