56

I have one dedicated server in that server I deployed 5 nodejs application.

domain name: www.nnd.com
dedicated server ip: xxx.xx.x.60

I had domain which is pointed to my dedicated server ip.

sub domains are :

app1.nnd.com pointed to xxx.xx.x.60
app2.nnd.com pointed to xxx.xx.x.60
app3.nnd.com pointed to xxx.xx.x.60
app4.nnd.com pointed to xxx.xx.x.60
app5.nnd.com pointed to xxx.xx.x.60

now in nginx configuration file based on the subdomain I need to route proxy. Example:

{
    listen:80;
    server_name:xxx.xx.x.60
    location / {
        #here based on subdomain of the request I need to create proxy_pass for my node application 
    }
}

Is there any condition and how can I get the original domain name from proxy header?

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
sridhar
  • 861
  • 3
  • 10
  • 14

3 Answers3

67

create a virtual host for each

server {
  server_name sub1.example.com;
  location / {
    proxy_pass http://127.0.0.1:xxxx;
  }
}
server {
  server_name sub2.example.com;
  location / {
    proxy_pass http://127.0.0.1:xxxx;
  }
}

And go on, change the port number to match the right port.

Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
  • 1
    i am just map the dedicated ip to my sub-domain.so domain name i hosted it out side and in that settings i mapped with my dedicated ip. – sridhar Jan 12 '14 at 05:35
  • so how can i get original proxy host name – sridhar Jan 12 '14 at 05:36
  • I think I'm missing something, isn't all the skin subdomains ponting to the same IP? What so you mean the original host name – Mohammad AbuShady Jan 12 '14 at 06:44
  • see when ever i ping app1.nnd.com address the request will come to my dedicated server xxx.xx.x.60 now the nginx $http_host variable contain my dedicated server ip xxx.xx.x.60, so now where can in get the app1.nnd.com which nginx variable might contain that information – sridhar Jan 17 '14 at 05:50
  • are you using the load balancing server block ? are the subdomains on a different server? – Mohammad AbuShady Jan 17 '14 at 06:08
  • subdomains on different server – sridhar Jan 17 '14 at 08:34
  • app1,app2,app3,app4,app5 all are different node sever running on different port in same dedicated server – sridhar Jan 17 '14 at 08:36
  • proxy_pass requires the url to be prefixed by the protocol on nginx 1.10.3, i had to add the prefixes to restart nginx, maybe its a problem of this version only, but you could add the http:// prefix to the example to make things more streamline on most versions, thanks ^^ – Nicolas NZ Oct 03 '17 at 22:09
24

You can use RegExp to fetch host name like this

server {
    server_name   ~^(www\.)?(?<domain>.+)$;

    location / {
        root   /sites/$domain;
    }
}
Chia-Yu Pai
  • 721
  • 1
  • 5
  • 19
7

You can create virtual host for every sub domain.

For Ex you have 2 sub domain abc.xyz.com and abcd.xyz.com , and you want to host it on nginx single instance by proxy_pass then you can simply create virtual host for every sub domain

server {
  server_name abc.xyz.com;
  location / {
    proxy_pass http://127.0.0.1:8000;
  }
}
server {
  server_name abcd.xyz.com;
  location / {
    proxy_pass http://127.0.0.1:8000;
  }
}

For more information you can refer here

abhaygarg12493
  • 1,565
  • 2
  • 20
  • 40