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 { dom1_backend;
        * dom2_backend;

    upstream dom1_backend {

    upstream pax_api_backend {
        server *;

    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 * will go to *

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; dom1_backend;
    * 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.