I'm running NGINX as a reverse proxy in front of some IIS hosted apps. My goal is to have a rewrite rule for a specific location. The thing is that the app itself responds with a 301 or 302 in some case. This delivers a new location header which I then need to address as well.
So basically what I try to do is this: - https://mydemo.app.de/abc should appear in browser but /xyz should be delivered instead of /abc. This works well, even if there is a redirect (301/302).
Here is my "half working" config:
location ~* /abc {
rewrite (?i)/abc/(.*) /xyz/$1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_pass http://mydemo.app.local;
proxy_redirect ~*(.*)\/xyz\/(.*) $1/abc/$2;
proxy_pass_header Server;
# Handle Web Socket connections
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
Now sometimes the following is returned by the upstream server: login.aspx?ReturnUrl=/xyz/ which is readable in the browser. This should not be the case. There should be /abc/ as well. As I understand it, the proxy_redirect part should handle this, but somehow doesn't in my case.
Is there any way I can get this to work without changes to the app itself (which would result in bigger changes...)?
Any help is much appreciated.
/abc/login.aspx?ReturnUrl=/xyz/
? Because, your regex will only substitute the first occurrence of/xyz/
. You would need to write a different expression to substitute two occurrences of/xyz/
.https://mydemo.app.de/abc/RANDOMID/login.aspx?ReturnUrl=/xyz/
. This should only include/abc/
in all occurrences.