nginx redirect based on domain name

Solution 1:

You need to setup a new vhost for example2.xyz.com. Nginx will read the domain name first then call respectively conf file otherwise default conf.

In nginx conf of vhost listen port 80 seperatly for both example1 and example2 or you can add listen 80 in default conf too for redirection to https.

Use map module for mapping multiple redirection like below example.

map $http_host $new {
  'exp1.xyz.com' '1';
  'exp2.xyz.com' '2';
}

server {
  listen 80;
  if ($new = '1') {
    rewrite ^(.*) https://exp1.xyz.com$1 redirect;
  }
  if ($new = '2') {
    rewrite ^(.*) https://exp2.xyz.com$1 redirect;
  }
}

For creating vhosts in nginx refer this link https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3