Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
Nginx Replace characters in hostname

I have urls like this:

  • https://foo.somedomain.com
  • https://foo.bar.somedomain.com
  • https://foo.bar.baz.somedomain.com
  • and so on

And I want nginx to replace dots "." characters in the subdomain part of the hostname by dashes "-", so this would result in :

  • https://foo.somedomain.com
  • https://foo-bar.somedomain.com
  • https://foo-bar-baz.somedomain.com
  • and so on

I already have a server block that catches the urls with dots in the "prefix" variable, but I could not find any documentation on how to rewrite url so that is replaces characters in "prefix" without using lua/...:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name "~^(?<prefix>[\w\.]+).somedomain.com$";

    rewrite 302 "$scheme://What do i put in here?.somedomain.com$request_uri";
}

A big thanks for people that will try to help on this!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You could capture both sides of the . with separate captures, for example:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name ~^([^.]+)\.([^.]+)\.([^.]+)\.example\.com$;

    ...

    return 302 $scheme://$1-$2-$3.example.com$request_uri;
}
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name ~^([^.]+)\.([^.]+)\.example\.com$;

    ...

    return 302 $scheme://$1-$2.example.com$request_uri;
}
over 4 years ago · Santiago Trujillo Denunciar

0

Use a map to capture the subdomains:

map $host $resub {
    ~^([^.]+)\.([^.]+)\.somedomain\.com$ $1-$2;
    ~^([^.]+)\.([^.]+)\.([^.]+)\.somedomain\.com$ $1-$2-$3;
    ~^([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)\.somedomain\.com$ $1-$2-$3-$4;
    ~^([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)\.somedomain\.com$ $1-$2-$3-$4-$5;
    # etc.
}

server {
    server_name  *.somedomain.com;

    if ($resub) {
        return 302 $scheme://$resub.somedomain.com$request_uri;
    }
}
over 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda