Search code examples
node.jsnginxsubdomain

Nginx proxy pass subdomain, node vhost


I have some trouble with nginx proxy_pass redirection on localhost subdomain. I have a domain "domain.com", i want to redirect all request on *.domain.com on *.localhost:9000. Then node handle all request on *.localhost:9000 to the good express app.

On nginx conf when i try the following :

server {
    server_name extranet.domain.com;
    listen 80;
    location / {
       proxy_pass http://extranet.localhost:9000;
    }
}

Request on extranet.domain.com are well redirected to the good express webapp.

With this :

server {
    server_name ~^(.*?)\.domain\.com$;
    listen 80;
    location / {
       proxy_pass http://localhost:9000/$1;
    }
}

Express app running on localhost:9000 handle request /mysubdomainname, which implie that regex is good.

But when i try :

server {
    server_name ~^(.*?)\.domain\.com$;
    listen 80;
    location / {
       proxy_pass http://$1.localhost:9000;
    }
}

All request on *.domain.com return http code 502. Why http://localhost:9000/$1; works and not http://$1.localhost:9000; ? (all subdomain are set in /etc/hosts).

Thanks in advance. I'm totally lost !


Solution

  • When a host name isn't known at run-time, nginx has to use its own resolver. Unlike the resolver provided by OS, it doesn't use your /etc/hosts file.