Nginx proxy_redirect
allows the use of variables such as $my_var
and regular expressions, prefixed with ~
.
But it seems impossible to use them both in combination. Is this possible, and what is the correct syntax for escaping meta-characters?
For example, I've tried:
proxy_redirect ~*https?://\\$proxy_host/(.*)$ /app1/$1
proxy_redirect ~*https?://\$proxy_host/(.*)$ /app1/$1
proxy_redirect ~*https?://$proxy_host/(.*)$ /app1/$1
Obviously the last example is incorrect, as the $
before proxy_host
is interpreted as a regex meta character. But the two other examples don't work either. (They aren't recognized and so don't rewrite the Location
header.
When I replace with just
proxy_redirect ~*https?://[^/] /(.*)$ /app1/$1
the redirect works just fine.
The issue is that this will redirect even for external redirects, so is too inclusive.
CodePudding user response:
Variables and regular expressions are mutually exclusive.
To match both http
and https
you could just use two consecutive statements, for example:
proxy_redirect http://$proxy_host/ /app1/;
proxy_redirect https://$proxy_host/ /app1/;
Anything following the matched string is automatically appended to the replacement string, so a regular expression is not necessary needed.