Nginx – How to make two domains to access a server and use two different homepage

domainnginxvarnish

I have a Digitalocean server with ubuntu linux, nginx 1.4.6 (running on port 80), varnish 3.0.5 (running on port 8080, together)
I have two domains, say siteA.com and siteB.com. In default.conf of nginx I configured so that the front door (80) use the siteA folder as root, the code is:

server {
    listen *:8080 default_server;

    root /home/sitea;
    index index.html index.htm index.php;

    server_name IP_domain_siteA;

    location / {
    autoindex on;
        autoindex_exact_size off;
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
     }    

     location ~ \.php$ {
        try_files $uri =404;
        expires off;
        fastcgi_read_timeout 900s;
        fastcgi_index index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

but I want the SiteB use the same port for access using the 2 domains to the same server. so when I from accessing:

siteA.com => carry my server folder:
/home/siteA/index.php
siteB.com => carry the same server folder (same ip as well):
/home/siteB/index.html

How I do it? already I tried everything, even including these backend lines in default.VCL (varnish configuration).

backend siteA{
     .host = "sitea.com";
     .port = "8080";
 }
 backend siteB{
      .host = "siteb.com";
      .port = "8080";
 }
 sub vcl_recv {
    if (req.http.host == "sitea.com") {
        #You will need the following line only if your backend has multiple virtual host names
        set req.http.host = "sitea.com";
        set req.backend = siteA;
        return (lookup);
    }
    if (req.http.host == "siteb.com") {
        #You will need the following line only if your backend has multiple virtual host names
        set req.http.host = "siteb.com";
        set req.backend = siteB;
        return (lookup);
    }
 }

It not resolved, it returns me the error:

BACKEND HOST "siteB.com": resolves to multiple IPv4 addresses. Only
one address is allowed.

i already use virtual hosts, for others folders with nginx but It is only possible for change PORTS, the line server_name directed to domainA or domainB, dont work…. because is the same IP

What can I do anyone have suggestions? thank you

EDIT 1:

nginx config for both sites is here (siteA):

server {
  listen *:8080 default_server;
  root /var/www/public/sitea;
  try_files $uri $uri/ @handler;
  index index.php index.html index.htm;

  # Make site accessible from http://localhost/
##domain address 1 of server...
server_name www.sitea.com.br sitea.com.br;  
  #location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    #try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
  #}

  ## These locations would be hidden by .htaccess normally
      location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
      location ^~ /media/downloadable/ { deny all; }
      location ^~ /pkginfo/            { deny all; }
      location ^~ /report/config.xml   { deny all; }
      location ^~ /var/                { deny all; }

      location /var/export/ { ## Allow admins only to view export folder
          auth_basic           "Restricted"; ## Message shown in login window
          auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
          autoindex            on;
      proxy_read_timeout 150;
    }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  location @handler { ## Magento uses a common front handler
         rewrite / /index.php;
      }

      location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
           rewrite ^(.*.php)/ $1 last;
      }


  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_read_timeout 120;
    include fastcgi_params;
  }


}

the other site (siteB):

server {
    listen 8090;

    client_max_body_size 20M;
    root /var/www/public/siteb;
    index index.html index.htm index.php;

    ##domain address 2 of server...
    server_name www.siteb.com.br siteb.com.br;

    location / {
        autoindex on;
        try_files $uri $uri/ /index.php?q=$request_uri;
        autoindex_exact_size off;
        proxy_pass http://localhost:8080;
     }

     location ~ \.php$ {
        #try_files $uri =404;
        expires off;
        fastcgi_read_timeout 900s;
        fastcgi_index index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

Best Answer

If you are using Varnish you can configure purge to the additional website, for example on default.vcl:

acl purge {
  "localhost";
  "127.0.0.1";
  "siteb.com";
}

and on nginx configuration:

server {
  listen 8080 default_server;
  server_name sitea.com;

  ...
}

server {
  listen 8080;
  server_name siteb.com;

  ...
}

If you cache both websites on varnish the nginx cannot differentiate correctly. Maybe has a better way but this also works.