2

i am currently in situation where i need to get/catch sub-domain and pass that sub-domain value to proxy_pass in Nginx config.

e.g.

if user enters

http://google.com.mydomain.com

then it should do proxy pass as

proxy_pass http://www.google.com/;

in above example google.com is sub-domain

can this be doable ? How can i achieve something like that in nginx ?

currently i am using configuration where sub-domain values are hard-coded in config files , but there are many sub-domains , so i need to do it like this way, but don't know correct syntax.

server {
    listen       80;
    server_name  subdomain.domain.com;
    charset utf-8;

    location / {
      proxy_pass http://www.subdomain/;
    }
}

I am using * as A record to redirect all sub-domains to my web-host, i.e. wildcard DNS.

update:

i have found code snippet from https://stackoverflow.com/a/22774520/1642018

server {          
    listen       80;                                               
  # this matches every subdomain of domain.
  server_name .domain.com;                                           

  location / {                                                   

    set $subdomain "";

    if ($host ~* "^(.+)\.domain.com$") {                             
      set $subdomain $1;                                         
    }                                                            

    proxy_pass http://$subdomain;   

  }                                                              
} 

but the request is showing my default page which is in my default web root.

Community
  • 1
  • 1
  • This looks like it might be a case of the XY Problem: http://xyproblem.info/. This is an unusual request. Why not just serve the subdomain? What exactly is the situation? There is most likely a far better solution than what you are thinking about – Dayo Aug 03 '15 at 18:04
  • @Dayo thanks for your reply, i wanted to use nginx as reverse proxy for any domain i want, so if i enter http://ebay.com.mydomain.com it should do proxy pass to ebay.com and act as reverese proxy and get a ebay.com page. hope this helps. –  Aug 03 '15 at 18:21
  • This will not with almost any domain. Especially with "big" ones like google or ebay – Alexey Ten Aug 03 '15 at 21:38

2 Answers2

6

Two things.

1- A resolver (a dns server for your nginx in order to resolve google.com, you may add at your hosts or you are able to add resolver statement)

2- You will need to resolve how your client will deals with different domains, I mean google.com is different than google.com.ar or google.fr)

At this example I made work it for your example google.com

worker_processes 4;

error_log /var/log/nginx/error.log;

events {
  worker_connections  1024;
}

http {

  server {
    listen       80;

    location / { 
      set $subdomain ""; 

      if ($host ~* "^(.+)\.domain.com$") {
        set $subdomain $1; 
      }   
      resolver 8.8.8.8;
      proxy_pass "http://$subdomain.com";

    }   
  }
}

I hope that this configuration helps to you.

Horacio
  • 2,865
  • 1
  • 14
  • 24
  • 1
    Do **NOT** use a publicly accessible DNS server such as `8.8.8.8`. [To prevent DNS spoofing, it is recommended configuring DNS servers in a properly secured trusted local network.](http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver) – Tim Oct 07 '16 at 20:16
  • Good point!, that was an example and should work for him, but if you want to avoid dns spoofing the best way is to have your own dns – Horacio Oct 08 '16 at 15:05
  • In my case another useful syntax was proxy_pass "http://$subdomain.domain.com"; – faysou May 22 '21 at 11:41
1

I would capture the subdomain using a map then proxy pass if the variable is defined:

map $host $subdomain {
    ~^(?<sub>.+)\.[^\.]+\.[^\.]+$ $sub;
}

server {          
    listen 80 default_server;
    server_name  _;

    location / {
        if ($subdomain) {
            proxy_pass http://$subdomain;   
        }                             
    }                                                              
}
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35