2

I am really bad with regex. I am trying to do this in nginx:

stream {

    map $ssl_preread_server_name $name {
        api.dom1.com dom1_backend;
        *.dom2.com dom2_backend;
    }

    upstream dom1_backend {
        server api.dom1.com:443;
    }

    upstream dom2_api_backend {
        server *.dom3.com:443;
    }

    server {
        listen 443;
        proxy_pass $name;
        ssl_preread on;
    }
}

How do I get dom2 working? I just need to map a wildcard domain back to wildcard domain.

Mean anything *.dom2.com will go to *.dom3.com

abubin
  • 21
  • 1
  • 3

2 Answers2

5

In order to use hostnames you should specify the special hostnames parameter to map. This allows for asterisk wildcards to be interpreted in the manner you expect.

map $ssl_preread_server_name $name {
    hostnames;
    api.dom1.com dom1_backend;
    *.dom2.com dom2_backend;
}
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Thanks! This works for the map block. What about the upstream block? Doesn't work with hostnames parameter. `server *.dom3.com:443;` – abubin Jul 03 '20 at 02:46
  • @abubin Huh? That doesn't even make any sense. There's no such host. You have to name a specific server. – Michael Hampton Jul 03 '20 at 03:01
  • oh..I wanted to make wildcard to wildcard. EG, aaa.dom2.com to aaa.dom3.com, bbb.dom2.com to bbb.dom3.com, x11.dom2.com to x11.dom3.com... is that even possible? Thanks. – abubin Jul 03 '20 at 04:26
0

I think with map you may won't need upstream any more, try this:

stream {
    map $ssl_preread_server_name $name {
        hostnames;
        api.dom1.com dom1_backend;
        *.dom2.com   dom2_backend;
    }

    server {
        listen 443;
        listen 6443;
        proxy_pass $name:$server_port;
        ssl_preread on;
    }
}
user5723841
  • 161
  • 1
  • 3