Opened 12 months ago

Last modified 12 months ago

#2291 accepted defect

Regex plus variable in Nginx `proxy_redirect`

Reported by: brsolomon-deloitte@… Owned by:
Priority: minor Milestone:
Component: documentation Version: 1.19.x
Keywords: Cc:
uname -a: Linux ingress-nginx-controller-54bfb9bb-qztrf 5.4.149-73.259.amzn2.x86_64 #1 SMP Mon Sep 27 12:48:12 UTC 2021 x86_64 Linux
nginx -V: nginx version: nginx/1.19.9
built by gcc 10.3.1 20210424 (Alpine 10.3.1_git20210424)
built with OpenSSL 1.1.1l 24 Aug 2021
TLS SNI support enabled

Description

It is not currently documented or apparent if it is possible to use a regex that also includes Nginx variables in proxy_redirect.

For example, none of these work:

proxy_redirect ~*https?://\\$proxy_host/(.*)$ /app1/$1
proxy_redirect ~*https?://\$proxy_host/(.*)$ /app1/$1
proxy_redirect ~*https?://$proxy_host/(.*)$ /app1/$1

This is described here in further detail: https://stackoverflow.com/q/70205048/7954504

The use-case for this is the scenario where one only wants to change Location header when the redirect location is for the internal app, not for an external redirect.

Change History (2)

comment:1 by brsolomon-deloitte@…, 12 months ago

It looks like the answer is no, based on https://github.com/openresty/replace-filter-nginx-module/issues/5. It seems worthwhile to document that.

comment:2 by Maxim Dounin, 12 months ago

Status: newaccepted

Agreed, it probably worth explicitly documenting that variables cannot be used within a regular expression, especially given that without regular expressions variables are allowed in the redirect string.

Note well that the original task can be better solved by a pair of proxy_redirect directives, without using regular expressions:

proxy_redirect http://$proxy_host/ /app1/;
proxy_redirect https://$proxy_host/ /app1/;

Further, proxy_redirect default; (which is the default) should work, though it will only handle the exact protocol used in proxying, not both http and https.

Note: See TracTickets for help on using tickets.