Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda