Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!