• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

200
Vistas
nginx url rewrite based off path

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).

over 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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
}
over 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda