1

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.

3
  • Is that /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/. Feb 24, 2020 at 11:45
  • Yes indeed. The returned URL would be https://mydemo.app.de/abc/RANDOMID/login.aspx?ReturnUrl=/xyz/. This should only include /abc/ in all occurrences. Feb 24, 2020 at 14:42
  • @RichardSmith, do you have another hint for me? It seems that nginx is not capable of dealing with the /g flag for multiple occurrences. Thanks Mar 2, 2020 at 7:50

1 Answer 1

0

If you have two instances of the text sequence /xyz/ that both need substituting with /abc/, you will need to compose a regular expression to match both and capture the three intervening gaps.

But, you will need to keep the existing proxy_redirect statement to handle the case where there is only one.

As with many Nginx directives that evaluate regular expressions, the order is probably significant, so I would place the more specific expression first.

For example:

proxy_redirect ~*(.*)/xyz/(.*)/xyz/(.*)$ $1/abc/$2/abc/$3;
proxy_redirect ~*(.*)/xyz/(.*)$ $1/abc/$2;
1
  • Yes, this did the trick for me. Thanks a ton. Mar 9, 2020 at 9:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .