nginx rewrite any sub-domain to specific URL

2019-05-21 07:37发布

问题:

I want to redirect the url for the following scenario in Nginx.

My domain name *.xyz.com

If the user send the request from reuest url -> abc.xyz.com

abc is not constant name.

Xyz.com is my domain and anything before the .xyz.com is redirecting to xyz.com/<abc> in the backend

In the backend request goes to abc.xyz.com to xyz.com/<abc>

But in the browser Url will be same. i.e. abc.xyz.com

回答1:

You could use a regular expression for your server_name and store the subdomain in a named capture. Then have a variable root:

server {
    server_name  ~^(?<subdomain>.+)\.xyz\.com$;
    root /var/www/$subdomain;
}