nginx stream map with wildcard

abubin asked:

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 pax_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

My answer:


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;
}

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.