1

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;
    }

}
  • Maybe this Stack Overflow question can help you: [Configure multiple sites with Varnish](https://stackoverflow.com/questions/3334023/configure-multiple-sites-with-varnish) – Paul Jan 09 '16 at 22:41
  • i already tried this, not work, not solved... this tip only work for subdomains, it's not my case – Matheus Silva Itep Jan 10 '16 at 06:56
  • It looks like you have Site A listening on 8080. Can you post your nginx config for both sites? You'll have to set up different server and location blocks to act as a reverse proxy. – Tim Jan 10 '16 at 18:51
  • editet with this content.... how works reverse proxy with same port for 2 homepages (two domains) of one server? thanks – Matheus Silva Itep Jan 11 '16 at 09:04

2 Answers2

1

The error that you are getting suggests that siteB.com has multiple IPv4 addresses. Rather than specifying your backend hosts using the DNS name, just use localhost or 127.0.0.1. That might placate varnish, and it eliminates a DNS lookup which might cause delays or (apparently as in this case) go wrong. I'm not too familiar with varnish myself but I would have additionally thought that you only need to specify a single backend (as it is in fact the same box here) and just make sure that the correct host headers are making it through to nginx. Speaking of which...

You don't need to set up any more proxies. Nginx will happily let you specify two virtual hosts listening on the same port, and will use the server_name directive to match against the host header of incoming requests. Any requests which don't match a known server_name will be caught by the default server block, usually the one specified first unless you override that with the default_server option on the listen directive. See the following example:

server {
  listen 8080;
  server_name siteA.com;

  ...
}

server {
  listen 8080 default_server;
  server_name siteB.com;

  ...
}

Here, both server blocks are listening to port 8080, and nginx knows which requests are for which site by matching the host header. If it gets a request it doesn't have a host match for, it would normally pass it to the block for siteA.com as the first defined block, but we specifically override that behaviour by declaring the block for siteB.com is the default server for requests on that port.

As your varnish config is ensuring that you set appropriate host headers before it passes requests to the backend, that should be all you need to do to serve multiple sites from it. I shouldn't have thought you'd even need to do that manually in varnish in this case, as you're not otherwise overwriting the headers; mostly that seems to be done for normalisation (i.e. collecting both www.site.com and site.com together so you don't cache two copies of the same thing). My reading suggests that you should not need to specify multiple backends either, as it's the same server on the same port you are looking at. Varnish should be smart enough to separate the caches based on different hosts.

Carcer
  • 957
  • 6
  • 12
1

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.

Eduardo
  • 126
  • 2