3

I use nginx as a reverse proxy for multiple Rails apps (using Nixy). I need to rewrite some requests to inject a string in front of url, if is missing.

For example: If I have http://proxy_ip/app_name/some/root I don't want to modify anything, but if I have http://proxy_ip/some/root I want to transform in http://proxy_ip/app_name/some/root

I tried rewrite ^ /app_name/$uri; but it doesn't work, and I have no idea how to conditionally do that rewrite.

AppyGG
  • 381
  • 1
  • 6
  • 12
kitz
  • 879
  • 2
  • 9
  • 24
  • Searching for your exact title turns up many hits right here on SO, have you tried any of the approaches described in any of them? [Example](https://stackoverflow.com/questions/45526436/nginx-conditional-rewrite-issue), [another example](https://stackoverflow.com/questions/40181475/nginx-conditional-redirect-to-https). – Don't Panic Oct 23 '19 at 09:38
  • Maybe https://stackoverflow.com/questions/40181475/nginx-conditional-redirect-to-https ? – AMDP Nov 16 '19 at 23:10
  • You may find an if statement example here: https://stackoverflow.com/questions/40181475/nginx-conditional-redirect-to-https – AMDP Nov 16 '19 at 23:11

2 Answers2

0

You could use a map to detect the missing app name, then return 301 with the missing app name.

map $uri $prepend_app_name {
    ~^/some/root(/.*)?$ app_name;
}

server {
    if ($prepend_app_name) {
        return 301 /$prepend_app_name$uri;
    }
    # ...
}
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
-1

Look at the comments and in case try with an if statement keeping in mind what nginx documentation advices: https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

AMDP
  • 327
  • 2
  • 12