0

How do I get the first letter of a variable in nginx

example

        server_name ~^(?<subdomain>\w+)\.development\.test$;

        location / {
                root /var/www/test/$subdomain.0/$subdomain;
                try_files $uri $uri/ =404;
        }

So this would work http://apple.development.test/ going to this path /var/www/test/a/apple

I have seen methods with map but not sure how to correctly do this

Dave M
  • 4,514
  • 22
  • 31
  • 30
MC-ifac
  • 3
  • 1

2 Answers2

1

Following approach might work too:

server_name ~^(?<subdomain>(?<firstletter>\w)\w+)\.development\.test$;

location / {
    root /var/www/test/$firstletter/$subdomain;
    try_files $uri $uri/ =404;
}
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

You can create a new variable with the first letter using the map module: http://nginx.org/en/docs/http/ngx_http_map_module.html#map

Example:

map $subdomain $firstletter {
    default                    "";
    "~^(?letter.{1}).*$"  $letter;
}
eKKiM
  • 1,540
  • 9
  • 23