I'm currently using openshift to host multiple apps and I want to be able to handle all the routing with nginx. Say I have the domain https://project.us.company.com
and the apps on openshift are like this:
https://foo-v1-project.eocp-apps.us.company.com
https://foo-v2-project.eocp-apps.us.company.com
I want to be able to do a reverse proxy
and rewrite like this:
https://project.us.company.com/foo/v1/test -> https://foo-v1-project.eocp-apps.us.company.com/test
https://project.us.company.com/foo/v2/test1 -> https://foo-v2-project.eocp-apps.us.company.com/test1
https://project.us.company.com/bar/v1/test?limit=5 -> https://bar-v1-project.eocp-apps.us.company.com/test?limit=5
https://project.us.company.com/bar/v2/test/test2 -> https://bar-v2-project.eocp-apps.us.company.com/test/test2
I also need some logic to make it work like this:
https://project.us.company.com/bar/test --> https://bar-v2-project.eocp-apps.us.company.com/test
So without a version it should default to a value (v2
in this case).
Try this:
# Assuming only three apps for now: foo, bar, baz
location ~ ^/((?:foo|bar|baz)(?:/v[0-9]+)?)$ {
return 301 /$1/;
}
rewrite ^/(foo|bar|baz)/((?:[^v].*|v[^_0-9].*|v[0-9]+(?:[^/0-9].*)?|v_(?:[^/].*)?)?)$ /$1/v_/$2;
# different default versions
rewrite /foo/v_/(.*) /foo/v2/$1 last;
rewrite /bar/v_/(.*) /bar/v4/$1 last;
rewrite /baz/v_/(.*) /baz/v8/$1 last;
location ~ /(foo|bar|baz)/(v[0-9]+)(/.*) {
proxy_pass https://$1-$2-project.eocp-apps.us.company.com$3
}