Nginx stream map with wildcard

nginxpcreregex

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

Best 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;
}
Related Topic