0

I have a domain and wildcard subdomains.

main domain: example.com
subdomains: s1.example.com, s2.example.com etc.

Now the requirements is

s1.example.com will be redirect to example.com/organization/s1

s2.example.com will be redirect to example.com/organization/s2

and For s1.example.com or s2.example.com my configuration is:

    upstream frontend {
       server example.com;
    }  

    server_name ~^(?<name>.+)\.example\.com$;

    location / {

    proxy_pass http://frontend;
    rewrite ^/$ /organization/$name break;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host example.com;
    proxy_cache_bypass $http_upgrade;
    }

Now I am not sure what i am missing in my config.

1 Answers1

0

This should work (not tested):

  server_name: *.example.com
    location / { 
        if ($host ~* (.*)\.domain\.com) {
            set $sub_domain $1;
            return 301 $scheme://example.com/organization/$sub_domain;
        }
    }

or using rewrite:

  server_name *.example.com;
  set $subdomain $1;
  rewrite ^(.*)$ $scheme://example.com/organization/$1;

I think it's better to separate the config of the subdomain (*.example.com) and the main domain (example.com). If you separate it you should not use proxy_pass in the subdomain because the config is only for redirection without content.